Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
A fast, lightweight (~12KB compressed) JavaScript logger with no runtime dependencies that is designed to mirror Apache Log4j 2 functionality.
A fast, lightweight (~12KB compressed) JavaScript logger with no runtime dependencies that is designed to mirror Apache Log4j 2 functionality.
Simply require the log4js2 module.
var log4js = require('log4js2');
Or, for HTML implementations, place the log4js distribution in your HTML <head>
tag.
<script type="text/javascript" src="log4js2.min.js"></script>
Then, log some stuff!!
// create the logger
var log = log4js.getLogger('myLogger');
// log an event
log.info('This is a log');
// output: "03-24-2016 12:00:18,670|myLogger:anonymous:3|This is a log"
Configure log4js using the configure() method. This must be the first thing you do. Otherwise, the first log you commit will not allow updates from this function
log4js.configure({
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S}|%logger:%M:%line|%message',
appenders : [ 'consoleAppender' ],
loggers : [ {
logLevel : log4js.LogLevel.INFO
} ],
allowAppenderInjection : true
});
Type: Boolean
Default: false
Turn on or off the ability to inject appenders. If set to false, no appenders can be added after the first log. This may be useful if you need to add an appender during runtime.
Type: Array.<String>
Default: [ 'consoleAppender' ]
Sets the appenders for the given log configuration. Packaged with log4js2 is the console appender. You can develop your own appenders to add more functionality.
Type: Array.<Object>
Default: [{ tag : 'main', logLevel : log4js.LogLevel.INFO }]
Sets the loggers for log4js2. The tag
property specifies what logger the appender pertains to (see below), and the logLevel
specifies the logging level (use log4js.LogLevel
).
log4js.configure({
// ...
loggers : [ {
logLevel : log4js.LogLevel.INFO
}, {
tag : 'debugLogger',
logLevel : log4js.LogLevel.DEBUG
} ]
});
var log = log4js.getLogger('myLogger');
var debugLog = log4js.getLogger('debugLogger');
log.debug('This message will not show');
debugLog.debug('This message will show');
Type: String
Default: "%d{HH:mm:ss} [%level] %logger - %message"
Sets the tagging layout for the logs. Refer to Apache's Log4j2 documentation for how to set the tag layout. Keep in mind that some of the layout tags are relatively more expensive, and should only really be used in a development environment - such as %method and %line. There are also a few layouts that are not implemented with log4js2:
log4js.configure({
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger.%M:%line - %message',
// ...
});
var log = log4js.getLogger('myLogger');
log.warn('This is a log {}', 'with parameters');
// output: 03-24-2016 16:04:41,440 [WARN] myLogger.anonymous:15 - This is a log with parameters
In order to make the %method tag word, you must call from named function, like so:
function callerFunction() {
log.info('This is within a name function');
}
// output: 03-24-2016 16:17:50,360 [INFO] myLogger.callerFunction:3 - This is within a name function
Otherwise, non-named functions will simply display an 'anonymous' placeholder:
var callerFunction = function () {
log.info('This is an anonymous function');
};
// outputs: 03-24-2016 16:19:42,373 [INFO] myLogger.anonymous:3 - This is an anonymous function
appender Object
Adds an appender (see configuration). The configuration option allowAppenderInjection must be set to true for this to work.
logger String [optional]
Gets a logger instance. If the logger is not set, the logger name will be pulled from the containing named instance it was created in (anonymous if unnamed).
logLevel Number
(use log4js.LogLevel)
logger String [optional]
Sets the log level for a specific logger, or all loggers (if logger is not set).
FAQs
[![Build Status](https://travis-ci.org/anigenero/log4js2.svg?branch=master)](https://travis-ci.org/anigenero/log4js2) [![codecov](https://codecov.io/gh/anigenero/log4js2/branch/master/graph/badge.svg)](https://codecov.io/gh/anigenero/log4js2)
The npm package log4js2 receives a total of 9 weekly downloads. As such, log4js2 popularity was classified as not popular.
We found that log4js2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.