To install
npm install --save logbok
To create a logger for a class
Or for a vanilla javascript function
...; ...
To log specific messages
logger.debug'this is a debug log';logger.info'this is an info log';logger.warn'this is a warn log';logger.error'this is an error log';
Configurations
Logbok reads the specified configuration from its CONFIG
property.
Currently supported configurable parameters are log-level
, disabled-loggers
, and log-appenders
.
ATTRIBUTE | TYPE | DESCRIPTION |
SAMPLE USAGE*** |
---|---|---|---|
log-level | string | can be either debug , info , warn , and error - with debug as the lowest and error as the highest level, respectively - initially set to debug . |
Logbok.CONFIG['log-level'] = 'debug' |
disabled-loggers | array | contains the list of the classes/types whose loggers will be disabled - initially set to empty. | Logbok.CONFIG['disabled-loggers'] = [MyClass] |
log-appenders | array | contains the list of instances of log appenders to be used - initially set to empty. Currently, Logbok provides 5 appenders, namely, ConsoleLogAppender , ChromeConsoleLogAppender , NodeConsoleLogAppender , IsoTimestampAppender , and RollingFileAppender |
Logbok.CONFIG['log-appenders'] = [ new ChromeConsoleLogAppender(), new CustomLogAppender() ] |
***Note: All of the above mentioned attributes are configured in a way that when a new value is assigned, the changes will be applied immediately.
Creating a custom log appender
If the provided appenders are insufficient, you can extend those and/or make your own log appender for your own specific usecase.
You just need to implement the Appender
interface like below:
// this is a blunt angular sample for a remote log appender implementation...// then somewhere up the initialization phaseLogbok.CONFIG =
For reference purposes, the appender interface is defined in Logbok as
Sample Codes
Vanilla javascript code snippet for Node JS:
var Logbok ConsoleLogAppender = ; var logger; { logger = logger || Logbok; logger;} LogbokCONFIG'log-appenders' = ; // prints something like "04/24/2019 - 15:30:29.958 INFO [myFunction] - this is an info log"
Code snippet for Typescript:
; Logbok.CONFIG = ;new MyClass.log; // prints something like "04/24/2019 - 15:46:33.891 INFO [MyClass] - this is an info log"
Log Appenders
Logbok comes with 5 core log appenders, namely, ConsoleLogAppender
, ChromeConsoleLogAppender
, NodeConsoleLogAppender
, IsoTimestampAppender
, and RollingFileAppender
. Each one has a different usecase but some can be used in conjunction with one another (e.g. RollingFileAppender
can be paired with either of the three: ConsoleLogAppender
, NodeConsoleLogAppender
, IsoTimestampAppender
).
-
#### ConsoleLogAppender This is the most basic log appender among all logbok appenders.
This one is built on top of the console object, hence the name.
No magic and all, just plain text pre-formatted to look like a decent log message.
Below is a sample snippet of the output of a ConsoleLogAppender. -
#### IsoTimestampAppender This one is time-formatted version of ConsoleLogAppender.
No magic and all, just plain text with pre-formatted time to look like a decent log message.
Below is a sample snippet of the output of a IsoTimestampAppender. -
#### ChromeConsoleLogAppender This one is a color-coded version of ConsoleLogAppender dedicated for Chrome/Chrome-based renderer apps (e.g. Electron)
Below is a sample snippet of the output of a ChromeConsoleLogAppender. -
#### NodeConsoleLogAppender This one is similar to ChromeConsoleLogAppender in the manner that it is also a color-coded version of ConsoleLogAppender.
The only difference is that this one is for the command line/terminal window console instead of the chrome.
Below is a sample snippet of the output of a NodeConsoleLogAppender. -
#### RollingFileAppender This one is more advanced compared to the other types of appender. The idea is that it redirects logs to a file instead of using a console object.
Currently, this can only be used by backend codes, as it requires collocation of the log file with the running instance.
This appender can be instantiated with the following optional parameters:NAME TYPE DESCRIPTION REMARKS filePath string the path of the file where to write the logs, creates it if it does not exist yet default value is './logbok.log' rolloverOption RolloverOption the rollover mechanism describing when to rename the current log file with a new rollover filename. see below description of possible values naming follows counting numbers starting from 1, e.g. for filepath='./server.log' rollover files will be server-1.log, server-2.log, and so on. declareBelow are sample rollover configurations
new RollingFileAppender'./server.log', // rolls over everytime log file reaches 10MiBnew RollingFileAppender'./server.log', // rolls over everytime log file reaches 100KiBnew RollingFileAppender'./server.log', // rolls over every 1 hournew RollingFileAppender'./server.log', // rolls over every 10 seconds