Express how to log and write files

Source: Internet
Author: User
1, the use of Express self-band module
var logFile = Fs.createwritestream ('./apkanalysis.log ', {
    "flags": "A"
});
App.configure (function () {
  App.use (Express.logger ({stream:logfile}));
};
This code is added to the app.js, but I found a problem, this log it only records other client access to the server information, the following figure is I use chrom access to my server

2. Use Winston module before using it, we have to install it in the project first: first go to the project folder, then NPM install Winston
Here we can quote it, I was introduced in the App.js:
var winston=require (' Winston ');
var logger = new (Winston. Logger) ({
    transports: [
        New (Winston.transports.Console) (),
        New (Winston.transports.File) ({ 
            FileName: './apkanalysis.log ',
            timestamp: ' True ', 
            maxsize:10485760,//log file size
            maxfiles:10}
    ]});
when we need to log files, we can write this: logger.log (' info ', ' Test logmessage ', {anything: ' This is metadata '});
when we runapp.js, we'll be there. ApkAnalysis.log See the following figure:

to make it easier for me to create a new config folder under the project directory, and then create a new file in the Config folder//logger.js
var winston=require (' Winston ');

var logger = new (Winston. Logger) ({
    transports: [
        New (Winston.transports.Console) (),
        New (Winston.transports.File) ({
            FileName: './apkanalysis.log ',
            timestamp: ' True ',
            maxsize:10485760,
            maxfiles:10}
    ]});

Exports.logger=logger;


It is convenient to call it later in other JS files, such as I call it in App.js, I will introduce it first: var logs=require ('./config/logger.js '); It's handy when you use it: logs.logger.log (' info ', "Express server Listeningon Port 3000");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.