0
votes

Azure application insights log messages using log4j framework in java as shown below.

https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-trace-logs

Is there any similar thing for nodejs application without using using azure node sdk to log messages. I am looking for logging log messages using log4js messages to App insights with some configuration changes.

1

1 Answers

0
votes

If you just need to log messages. Your can use log package for nodejs.

Installation:

npm i log

This is Universal logging utility Configurable, environment and presentation agnostic, with log levels and namespacing (debug style) suppor

Usage Writing logs

// Default logger writes at 'info' level
const log = require("log");

// Log 'info' level message:
log("some info message %s", "injected string");

// Get namespaced logger (debug lib style)
log = log.get("my-lib");

// Log 'info' level message in context of 'my-lib' namespace:
log("some info message in 'my-lib' namespace context");

// Namespaces can be nested
log = log.get("func");

// Log 'info' level message in context of 'my-lib:func' namespace:
log("some info message in 'my-lib:func' namespace context");

// Log 'error' level message in context of 'my-lib:func' namespace:
log.error("some error message");

// log output can be dynamically enabled/disabled during runtime
const { restore } = log.error.disable();
log.error("error message not really logged");
// Restore previous logs visibiity state
restore();
log.error("error message to be logged");

Available log levels

Mirror of applicable syslog levels (in severity order):

debug - debugging information (hidden by default) info - a purely informational message (hidden by default) notice - condition normal, but significant warning (also aliased as warn) - condition warning error - condition error - to notify of errors accompanied with recovery mechanism (hence reported as log and not as uncaught exception) Note: critical, alert, emergency are not exposed as seem to not serve a use case in context of JS applications, such errors should be exposed as typical exceptions

Output message formatting log doesn't force any specific arguments handling. Still it is recommended to assume printf-like message format, as all currently available writers are setup to support it. Placeholders support reflects one implemented in Node.js format util

Excerpt from Node.js documentation:

The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument. Supported placeholders are: %s - String.
%d - Number (integer or floating point value).
%i - Integer.
%f - Floating point value.
%j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.
%o - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, depth: 4, showProxy: true }. This will show the full object including non-enumerable symbols and properties.
%O - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable symbols and properties.
%% - single percent sign ('%'). This does not consume an argument. Note to log writer configuration developers: For cross-env compatibility it is advised to base implementation on sprintf-kit

Enabling log writing log on its own doesn't write anything to the console or any other means (it just emits events to be consumed by preloaded log writers).

To have logs written, the pre-chosen log writer needs to be initialized in the main (starting) module of a process.

List of available log writers log-node - For typical Node.js processes log-aws-lambda - For AWS lambda environment Note: if some writer is missing, propose a PR

Logs Visibility Default visibility depends on the enviroment (see chosen log writer for more information), and in most cases is setup through the following environment variables:

LOG_LEVEL (defaults to notice) Lowest log level from which (upwards) all logs will be exposed.

LOG_DEBUG Eventual list of namespaces to expose at levels below LOG_LEVEL threshold

List is comma separated as e.g. foo,-foo:bar (expose all foo but not foo:bar).

It follows convention configured within debug. To ease eventual migration from debug, configuration fallbacks to DEBUG env var if LOG_DEBUG is not present.

Timestamps logging Writers are recommended to to expose timestamps aside each log when following env var is set

LOG_TIME rel (default) - Logs time elapsed since logger initialization abs - Logs absolute time in ISO 8601 format Tests $ npm test Project cross-browser compatibility supported by: