Feel Logger
This is my default configuration for winston logger.
Table of contents
- Motivation
- Quick Start
- Outpu Format
- Configuration
- Dynamic Initialization
- Logger Interface
- Loggin Context
- Logger Instance
- Wisnton Instance
Motivation
Winston
is an incredible and versatile logging library. To simplify the configuration process and keep the logs consistent across projects, i decided to create this package to easily setup winston with my formats and configurations.
This package has two default transports: Console
and Dayle Rotate File
.
Quick Start
You just need to import Logger
and call the init
method and a Console
logger will be created.
; Logger;Logger;// output: 2020-07-12 13:43:05 [Application] INFO: My Awesome Logger!
To activate the Daily Rotate File
just pass a configuration object
to init
method.
; Logger;Logger;
This will create a /logs
folder in the project root and store there the logs files.
Output Format
The output format is composed by a timestamp
, a identifier label
, the log level
, the log message
and the associated metadata
.
YYYY-MM-DD HH:MM:SS label LEVEL: message Meta: ...
Configuration
The configurations accepted by the Logger are:
property | type | default | description |
---|---|---|---|
name | string | Application | this will be the label |
level | string | info | the log level |
exitOnError | boolean | false | |
handleException | boolean | false | |
transports | Winston.Transports[] |
Console , Daily Rotate File |
custom transports to be add to the Logger |
file | boolean | false | active Dayle Rotate File transport |
fileOptions | DailyRotateFileTransportOptions |
null | overrides default transport options |
console | WinstonLoggerOptions |
null | overrides default transport options |
Dynamic Initialization
To make the Logger instance static
across the application you have to create a logger.options
file. When the lib is loaded by node it will be search for a configuration file (.js or .ts) in the project. This dynamic initialization overrides init
method.
+-- root
| +-- src
| | +-- configuration.options.[ts|js]
| +-- package.json
The configuration file accepts the same object as the init
method, but make sure that your logger.options.[ts|js]
is inside a src
folder under the root
directory and follow the conventions:
/* logger.options.js */moduleexports = file: true;
/* logger.options.ts */ file: true;
Now you can import Logger
in any file and use without pass through injection.
/* AccountController */; { Logger; }
Logger Interface
The following is the methods for logging:
interface LoggerOperations void; void; void; void; void;
Logging Context
If you want to add a context information to the output log you can pass a special metadata called context
and it will be add to the label
part of the log.
; Logger;// output: 2020-07-12 14:42:29 [Application - MyContext] INFO: My Awesome Logger
Logger Instance
If you initialize your Logger
dynamically, you can create instances of the logger to set a default context
.
; const logger = Logger;logger;// output: 2020-07-12 14:47:31 [Application - MyDefaultContext] INFO: My Awesome Logger
You can pass a class
type as well.
; logger = Logger; { thislogger; } ;// output: 2020-07-12 14:47:31 [Application - MyAwesomeClass] INFO: My Awesome Logger
Winston Instance
You can get the winston
instance object by calling the method:
;const winston = Logger;