Skip to content

Logging

Suraj kc edited this page Jun 30, 2020 · 1 revision

Logging

Logging is the process of writing information into log files. Log files include information about various events that happened int the system

Purpose of logging

  • Troubleshooting
  • Information gathering
  • Profiling
  • Auditing
  • Generating statistics

It helps in identifying errors. It is also used in detecting security incidents, application bottlenecks and generating usage data.

Events to log

  • Input validation failures
  • Authentication and authorization failures
  • Application errors
  • Configuration changes
  • Application startup and shutdowns

Events not to log

  • Application source code
  • Session identification values
  • Access tokens
  • Passwords
  • Personal data
  • Database connection strings
  • Encrypted values

Best practices

  • Structured and done at different levels
  • Contain context
  • Understandable to humans and parsable by machines
  • Adapted to development and production
  • Can be divided into several log files

Writing log messages

logger.info('server.endpoint.get.users.getusersfromdb.format');
const handler = (func) => (req, res) => {
    try {
        logger.info('server.handler.begun');
        func(req, res, logger);
    } catch(e){
        logger.info('server.handler.failed');
        res.send('Oh no, something did not go well!');
    }
};

More links:

Ref: