How to use Morgan logger?

后端 未结 10 1618
囚心锁ツ
囚心锁ツ 2020-12-07 11:19

I cannot log with Morgan. It doesn\'t log info to console. The documentation doesn\'t tell how to use it.

I want to see what a variable is. This is a code from

相关标签:
10条回答
  • 2020-12-07 12:08

    I faced the same problem ago and instead, I used winston. As fellas above said, morgan is for automated logging of request/response. Winston can be configured pretty much same way as log4Net/log4J, has severity levels, different streams to which you can log etc.

    For example:

    npm install winston

    Then, if you call the below code somewhere on you application initialization:

    var winston = require('winston');
    
    // setup default logger (no category)
    winston.loggers.add('default', {
        console: {
            colorize: 'true',
            handleExceptions: true,
            json: false,
            level: 'silly',
            label: 'default',
        },
        file: {
            filename: 'some/path/where/the/log/file/reside/default.log',
            level: 'silly',
            json: false,
            handleExceptions: true,
        },
    });
    
    //
    // setup logger for category `usersessions`
    // you can define as many looggers as you like
    //
    winston.loggers.add('usersessions', {
        console: {
            level: 'silly',
            colorize: 'true',
            label: 'usersessions',
            json: false,
            handleExceptions: true,
        },
        file: {
            filename: 'some/path/where/the/log/file/reside/usersessions.log',
            level: 'silly',
            json: false,
            handleExceptions: true,
        },
    });
    

    note: before calling above code, winston.loggers is empty, i.e you dont have any loggers configured yet. Pretty much like Log4Net/J XmlConfigure methods - you need to first call them, to init your logging.

    Then, later wherever in you application server side code you may do:

    var winston = require('winston');
    // log instances as defined in first snippet
    var defaultLog = winston.loggers.get('default'); 
    var userSessionsLog = winston.loggers.get('usersessions');
    
    defaultLog.info('this goes to file default.log');
    userSessionsLog.debug('this goes to file usersessions.log')
    

    Hope that helps.

    for further documentation reference: https://www.npmjs.com/package/winston

    0 讨论(0)
  • 2020-12-07 12:11

    Just do this:

    app.use(morgan('tiny'));
    

    and it will work.

    0 讨论(0)
  • 2020-12-07 12:17

    Morgan :- Morgan is a middleware which will help us to identify the clients who are accessing our application. Basically a logger.

    To Use Morgan, We need to follow below steps :-

    1. Install the morgan using below command:

    npm install --save morgan

    This will add morgan to json.package file

    1. Include the morgan in your project

    var morgan = require('morgan');

    3> // create a write stream (in append mode)

    var accessLogStream = fs.createWriteStream(
          path.join(__dirname, 'access.log'), {flags: 'a'}
     );
    // setup the logger 
    app.use(morgan('combined', {stream: accessLogStream}));
    

    Note: Make sure you do not plumb above blindly make sure you have every conditions where you need .

    Above will automatically create a access.log file to your root once user will access your app.

    0 讨论(0)
  • 2020-12-07 12:18

    You might want to try using mongo-morgan-ext

    The usage is:

    var logger = require('mongo-morgan-ext');
    
    var db = 'mongodb://localhost:27017/MyDB';
    
    var collection = 'Logs'
    
    var skipfunction = function(req, res) {
    
    return res.statusCode > 399;
    } //Thiw would skip if HTTP request response is less than 399 i.e no errors.
    
    app.use(logger(db,collection,skipfunction)); //In your express-application
    

    The expected output is

    {
        "RequestID": "",
        "status": "",
        "method": "",
        "Remote-user": "",
        "Remote-address": "",
        "URL": "",
        "HTTPversion": "",
        "Response-time": "",
        "date":"",
        "Referrer": "",
        "REQUEST": { //10
            "Accept": "",
            "Accept-Charset": "",
            "Accept-Encoding": "",
            "Accept-Language": "",
            "Authorization": "",
            "Cache-Control": "",
            "Connection": "",
            "Cookie": "",
            "Content-Length": "",
            "Content-MD5": "",
            "Content-Type": "",
            "Expect": "",
            "Forwarded": "",
            "From": "",
            "Host": "",
            "Max-Forwards": "",
            "Origin": "",
            "Pragma": "",
            "Proxy-Authorization": "",
            "Range": "",
            "TE": "",
            "User-Agent": "",
            "Via": "",
            "Warning": "",
            "Upgrade": "",
            "Referer": "",
            "Date": "",
            "X-requested-with": "",
            "X-Csrf-Token": "",
            "X-UIDH": "",
            "Proxy-Connection": "",
            "X-Wap-Profile": "",
            "X-ATT-DeviceId": "",
            "X-Http-Method-Override":"",
            "Front-End-Https": "",
            "X-Forwarded-Proto": "",
            "X-Forwarded-Host": "",
            "X-Forwarded-For": "",
            "DNT": "",
            "Accept-Datetime": "",
            "If-Match": "",
            "If-Modified-Since": "",
            "If-None-Match": "",
            "If-Range": "",
            "If-Unmodified-Since": ""
        },
        "RESPONSE": {
            "Status": "",
            "Content-MD5":"",
            "X-Frame-Options": "",
            "Accept-Ranges": "",
            "Age": "",
            "Allow": "",
            "Cache-Control": "",
            "Connection": "",
            "Content-Disposition": "",
            "Content-Encoding": "",
            "Content-Language": "",
            "Content-Length": "",
            "Content-Location": "",
            "Content-Range": "",
            "Content-Type":"",
            "Date":"",
            "Last-Modified": "",
            "Link": "",
            "Location": "",
            "P3P": "",
            "Pragma": "",
            "Proxy-Authenticate": "",
            "Public-Key-Pins": "",
            "Retry-After": "",
            "Server": "",
            "Trailer": "",
            "Transfer-Encoding": "",
            "TSV": "",
            "Upgrade": "",
            "Vary": "",
            "Via": "",
            "Warning": "",
            "WWW-Authenticate": "",
            "Expires": "",
            "Set-Cookie": "",
            "Strict-Transport-Security": "",
            "Refresh":"",
            "Access-Control-Allow-Origin": "",
            "X-XSS-Protection": "",
            "X-WebKit-CSP":"",
            "X-Content-Security-Policy": "",
            "Content-Security-Policy": "",
            "X-Content-Type-Options": "",
            "X-Powered-By": "",
            "X-UA-Compatible": "",
            "X-Content-Duration": "",
            "Upgrade-Insecure-Requests": "",
            "X-Request-ID": "",
            "ETag": "",
            "Accept-Patch": ""
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题