neo-log4js v0.0.2
Neo-Log4js
Neo-Log4js - The new and enhanced logging Framework similar to log4j, written in typescript for modern web applications
Features
- Supports different layouts like
- Simple Layout
- HTML Layout
- XML Layout
- JSON Layout
- Pattern Layout
- Basic Layout
- Support different appenders like
- Ajax Appender
- Browser Console Appender
- JS Alert Appender
- Some appenders supports log buffering with configurable threshold.
- Can enable or disable logging on the fly
- Highly configurable with a JSON based structure for for overriding the defaults of loggers, appender and layout options.
Installation
npm install --save-dev neo-log4js
Configuration
1. Configure using Configurator JSON
A sample JSON configurator is shown below
{
"rootLogger":"INFO",
"logger":[{"categoryName":"module1","logLevel":"INFO"},{"categoryName":"module2","logLevel":"DEBUG"}],
"appenders":[{
"name":"console",
"layoutObj":{
"name":"pattern",
"conversionPattern":"%d{yyyy-MM-dd HH:mm:ss} %p %c: %m%n"
}
},
{
"name":"ajax",
"url":"/logging/ajax",
"threshold":10,
"layout":"html"
}]
}
The various appender names acceptable are
- ajax
- console
- js
The various layout names acceptable to the configurator are
- html
- xml
- json
- basic
- simple
pattern
For pattern layout additional configuration objects can be set using the layoutObj property. The log level for different categories or modules can be set in advance using the logger property of the JSON structure.
The JSON object can be set to the Logger as shown below in javascript
let loggerProperties = {
"rootLogger":"INFO",
"logger":[{"categoryName":"home","logLevel":"WARN"},{"categoryName":"dashboard","logLevel":"DEBUG"}],
"appenders":[{
"name":"console",
"layout":"simple"
}]
};
Log4js.setLoggerJSON(loggerProperties);
let logger:Logger = Log4js.getLogger("dashboard");
...
...
this.logger.info("This is an info log");
2. Using exposed API's
Sample usage for browser console logging as well as ajax is shown below.
Browser Console Appender
Initial configuration
var consoleLog = Log4js.getLogger("consoleTest");
consoleLog.setLevel(Log4js.Level.ALL);//default is ERROR, if not set
var consoleAppender = new Log4js.BrowserConsoleAppender();
consoleAppender.setLayout(new Log4js.BasicLayout());//default is SimpleLayout, if not set
consoleLog.addAppender(consoleAppender);
Usage
this.consoleLog.info(`Invoked method calulateKPIData with argument ${0}, ${1} and ${2}`);
Ajax Appender
Inital configuration
var ajaxLog = Log4js.getLogger("ajaxTest");
ajaxLog.setLevel(Log4js.Level.ALL);//default is ERROR, if not set
var ajaxAppender = new Log4js.AjaxAppender( "/logging/log4js");//REST endpoint
ajaxAppender.setThreshold(50);//default is 5 , if not set
ajaxAppender.setLayout(new Log4js.XMLLayout());// default is JSONLayout, if not set
ajaxLog.addAppender(ajaxAppender);
Usage
this.ajaxLog.info(`Invoked method calulateKPIData with argument ${0}, ${1} and ${2}`);
Details of Layout implementations
Pattern Layout
The Pattern Layout is highly customizable form of logging Layout. The default pattern configured for this layout manager will log only the messages. You can supply predefined custom pattern's using the constructor as shown below
new PatternLayout(PatternLayout.TPC_CONVERSION_PATTERN);
The predefined custom patterns are 1. DEFAULT_CONVERSION_PATTERN 2. TPC_CONVERSION_PATTERN 3. DCP_CONVERSION_PATTERN
In addition, custom patterns can be supplied to constructor as shown below
new PatternLayout("%d{yyyy.MM.dd.HH:mm:ss.0} %c [%p] : %m%n");
The pattern character supported are
character | description | example |
---|---|---|
c | category name | default, or user supplied name |
p | log level | DEBUG, INFO etc |
r | time in locale time format | "7:00:00 PM" locale is en-US |
d | datetime format | default is ISO8601 |
m | message | message to be logged |
n | newline |
Details of Appenders
Browser Console Appender
The Browser console appender writes the log statements to the browsers console window. It will format the log lines based on the layout set as well as color the log statements based on the log level. The default log level is set as ERROR and users can override it by using the setLevel API. The default layout set for the appender is SimpleLayout, however it can be overridden by using the setLayout API
Ajax Appender
The Ajax appender buffer the log events based on a threshbold and invokes the REST endpoint passed to the constructor. The default buffering threshold is 5 which can be modified by using the setThreshold API. The default log level is set as ERROR and users can override it by using the setLevel API. The default layout set for the appender is JSONLayout, however it can be overridden by using the setLayout API
API
Following are the exposed functions, which can be accessed using Log4js.functionName .
- getLogger(categoryName?:string) - returns the Logger instance registered for the categoryName. if no categoryName is supplied, the for root logger will be returned.
- enable(categoryName?:string) - enable complete logging for all logs of this category, if no categoryName is given, then it will enable the root logger.
- enableAll() - enable logging of all the loggers
- disable(categoryName?:string) - disable complete logging for all logs of this category, if no categoryName is given, then it will disable the root logger.
- disableAll() - disable logging of all the loggers
- setLoggerJSON(loggerPropertiesJSONString :string) - set the json string supplied with logger properties for the logger configuration.
Following are the exposed functions, which can be accessed using Log4js.className .
- AjaxAppender
- BrowserConsoleAppender
- JSAlertAppender
- PatternLayout
- HtmlLayout
- JSONLayout
- XMLLayout
- BasicLayout
- SimpleLayout
- Level
TODO's
- Exception information enhancement using stacktrace.js
- A configurator object which will take a JSON object for overriding the defaults of loggers, appender and layout options
- A configuration Ui component using web components for live enabling, disabling and re-configuration.
- Option to download log lines to client browser as file.
- Configuration for sending mail of log messages to configured recipients using default mail client or using libraries like SmptJS or
- Improve the documentation
- Add more relevant test cases.
- Identify and add more appenders and thus cover more use-cases.
- Integration with logstash/lumberjack, loggly etc
- write wrapper for Angular 2 or higher , reactJS frameworks.
- Include Logging Aspect using aspect.js