log4jcore

logging

Usage no npm install needed!

<script type="module">
  import log4jcore from 'https://cdn.skypack.dev/log4jcore';
</script>

README

log4jcore

log4jcore

Installation

yarn add log4jcore

Usage

import { logger } from 'log4jcore'

const log = logger('MyClass')

log.info('starting application')

// Multiple arguments are supported, like with console.log. 
// One benefit of this approach is that arguments are not 
// stringified unless the log level requires the message to be
// logged.
log.info('ip address:', process.env.IP_ADDRESS)

// Use arrow functions to avoid computing a message unless the
// log level requires it to be logged.
log.debug(() => `hostname: ${process.env.HOSTNAME}`)

Configuring Log Levels

Log levels are set to info by default. To change the log level for a logger, pass in an environment variable like DEBUG=MyClass.

Valid environment variable names are:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

To change the log levels of multiple loggers, use a comma separated list of logger names, like DEBUG=MyClass,AnotherClass.

Changing Appenders

The default appender calls console.log if the message severity is WRAN or lower, and calls console.error if the message severity is ERROR or higher.

setLogFunctionProvider allows you to override this and replace the log writer with a different one. This can be useful if you want to log to a file or log to an external logging API.

Logging to a file using log4jcore-file-appender:

log4jcore-file-appender is a simple logging provider that writes to a file instead of stdout / stderr. At the time of this writing, it does not have TypeScript type defs although it does have Flow Type defs.

yarn add log4jcore-file-appender
const { setLogFunctionProvider } = require('log4jcore')
const { createFileAppender } = require('log4jcore-file-appender')

setLogFunctionProvider(createFileAppender({ file: 'messages.log' }))