Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »


2.0.0-alpha1 • Public • Published

Build Status codecov


log4js2 is a fast and lightweight logging library that enables logging flexibility within JavaScript/TypeScript applications, similar to Apache's Log4j2 library. This library is designed to mirror Log4j2 functionality to the best ability that a JavaScript framework can. It can also serve as a drop-in replacement for log4js, since the namespace and functions are mostly similar.

Installing & Building

If you're building from source, simply run

> npm install
> npm run build

Or, you can install log4js2 from npm.

npm install log4js2 --save


Import log4js from the module:

import * as log4js from 'log4js2';


Logging works out-of-the-box, with no configuration, however, note that only error messages will display without specific configuration

const logger = log4js.getLogger('myLogger');
logger.error('This is a log');

Default layout will follow the pattern layout of %d [%p] %c - %m

03-24-2016 12:00:18,670 [ERROR] myLogger - 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

import {configure, LogLevel} from 'log4js2';
    layout : '%d [%p] %c %M:%line:%column - %m %ex',
    appenders : [{
        appender: 'Console'
    loggers : [{
        tag: 'App',
        logLevel : LogLevel.INFO

Virtual Console

ConsoleAppender utilizes a virtual console that "hijacks" console and inputs it into the log4js2 stream. Make sure log4js2 is loaded at the top of the page to ensure all logs are caught.

console.log('console log');
// outputs: 08-30-2018 12:38:00 [INFO] main - console log

Configuration Options


Type: Array.<AppenderConfiguration> Default: [{ appender: 'Console' }]

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.<LoggerConfiguration> Default: [{ logLevel : log4js.LogLevel.ERROR }]

Specifies 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). You can also set a logger-specific layout using the patternLayout property.

    // ...
    loggers : [ {
        logLevel : LogLevel.INFO
    }, {
        tag : 'debugLogger',
        logLevel : LogLevel.DEBUG
const logger = getLogger('myLogger');
const debugLogger = getLogger('debugLogger');
logger.debug('This message will not show');
debugLogger.debug('This message will show');


Type: String Default: "%d [%p] %c - %m"

Sets the pattern layout for the logs. 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. Refer to the wiki for more information.

    patternLayout : '%d{MM-dd-yyyy HH:mm:ss} [%p] %c.%M:%line - %message',
    // ...
const logger = getLogger('myLogger');
logger.warn('This is a log {}', 'with parameters');
03-24-2016 16:04:41 [WARN] myLogger.anonymous:15 - This is a log with parameters
Note: Showing Method Names

In order to make the %method tag word, you must call from named function, like so:

function callerFunction() {'This is within a name function');
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:

let callerFunction = function () {'This is an anonymous function');
03-24-2016 16:19:42,373 [INFO] myLogger.anonymous:3 - This is an anonymous function

Custom Appenders

You can output logs to a specific location or methodology using a custom appender. The @Appender decorator will handle registering the appender with log4js2.

    name: 'CustomAppender'
class MyAppender extends LogAppender {
    static get name() {
        return 'myappender';
    append(logEvent: LogEvent) {
        const message: string = this.format(logEvent);
        // ... handle formatted result



configuration Configuration

Sets the configuration. If no configuration is set before the first log, then the default configuration will be used. See configuration for options.


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).

setLogLevel(logLevel, logger)

logLevel LogLevel|Number, logger? String

Sets the log level for a specific logger, or all loggers (if logger is not set).


npm i log4js2

Downloadsweekly downloads









last publish


  • avatar
Report a vulnerability