thunder-log
v1.0.2
Published
The logger used for Shared Services Logging.
Downloads
10
Maintainers
Readme
thunder-log
Authors: Greg Morgan + Ryan Cooper
Built for Vertafore Shared Services logging in javascript.
About thunder-log
Implementing winston, a logger for javascript applications, thunder-log is a wrapper around this which handles the necessary fields, formatting, and transports for our team.
How to use thunder-log
Creating a logger in your project
Install thunder-log into the project
npm install --save thunder-log
Once installed in the project, add thunder-log into a file and call the logger
const thunder-log = require('thunder-log');
const logger = thunder-log.getLogger();
getLogger()
The call for a logger can take no arguments, determining the appropriate logger based on the node environment, or it can be passed the name of the logger needed.
thunder-log.getLogger('development');
// returns a logger which will print to console and file (log.txt) set to a level of debug
thunder-log.getLogger('production');
// return a logger which print json formatted logs to console set to a level of info
thunder-log.getLogger('test');
// return a test logger which overrides the functions of the logger to use in testing with a level of debug
Logging levels
thunder-log implements several standard logging levels such as:
- debug
- info
- warn
- error
The level of the logger determine which logs are passed to the transport to be recorded.
Loggers of these levels will only print messages of level equal to them or lower on the list.
ie: a logger of level 'info' does not print debug statements but will print warn and error statement
A basic logging statement
logger.write('info', 'This is my first info');
Alternatively;
logger.info('This is also my first info');
thunder-log supports both methods of writing a logging statement. This is true as well for debug, warn, and error statements with the exception of error statements only accepting an object and not a string.
logger.write('debug', 'I am debugging');
logger.debug('I am also debugging');
logger.write('warn', 'I am warning');
logger.warn('I am also warning');
Error logging
Errors require an object of two fields (Summary and ExceptionDetails) in order to make a valid logging statement
logger.write('error', { Summary: 'This is my error', ExceptionDetails: 'This is my error message' });
logger.error({ Summary: 'This is my error', ExceptionDetails: 'This is my error message' });
Accepted Fields
All logging levels can take in additional fields depending on the needs of the statement.
- CorrelationId (string) Attached to all requests sent or generated for all responses, used for tracking http request/responses throughout their lifetime
- SequenceNumber (int) The iteration of a corresponds to a CorrelationId
- ApplicationId* (string) Applied to logs of a given application ex: 'VMG' for Vertafore Mobile Gateway logs
- Severity* (string) The severity of the log
- Timestamp* (dateTime) UTC, The time which the log was made
- LoggerId (string) The component responsible for where the logging took place; a method name, a class, or a file
- UserId (string) Identifier for a particular user (see PII requirements to not post any information that can be tracked back to the user)
- Summary (string) Detailed description of the log
- Action (string) Http request verbs; GET, POST, DELETE, etc
- ActionTarget (string) What module, service, or class is the target of the operation
- ActionResult (string) What was the result of the action; success, fail, timeout
- ExceptionDetails (string) Contains the exception code corresponding to the message passed using the Shared Services Exceptions Handling Schema ex: '400: Invalid Input Error'
- StackInfo (string) When errors occur, can provide a stack trace in the log
- AdditionalDetails (string) Information which should be provided but does not fit a category above
* Fields which are handled by the logger and do not need to be passed into the logging statement
Examples
These examples function as templates for logging statements.
Info statement with all generally used fields
logger.write('info',
{
CorrelationId: '',
SequenceNumber: '',
LoggerId: '',
UserId: '',
Summary: '',
Action: '',
ActionTarget: '',
ActionResult: '',
AdditionalDetails: ''
}
);
Error statement with inclusion of required error fields
logger.write('error',
{
CorrelationId: '',
SequenceNumber: '',
LoggerId: '',
UserId: '',
Summary: '',
Action: '',
ActionTarget: '',
ActionResult: '',
ExceptionDetails: '',
StackInfo: '',
AdditionalDetails: ''
}
);