Simple JavaScript logger written in TypeScript that can be used in browser or node application.
The package is most useful when used with TypeScript.
npm install simplr-loggerimport{LoggerBuilder}from"simplr-logger";constlogger=newLoggerBuilder();import{LoggerBuilder,LoggerConfigurationBuilder,LogLevel}from"simplr-logger";import{FileMessageHandler,ConsoleMessageHandler}from"simplr-logger/handlers";constconfig=newLoggerConfigurationBuilder().SetDefaultLogLevel(LogLevel.Trace).AddWriteMessageHandlers([{Handler: newConsoleMessageHandler()},{Handler: newFileMessageHandler("./logs.txt")}]).Build();constlogger=newLoggerBuilder(config);import{LoggerBuilder,LogLevel,ConsoleMessageHandler}from"simplr-logger";constlogger=newLoggerBuilder({DefaultLogLevel: {LogLevel: LogLevel.Trace,LogLevelIsBitMask: false},WriteMessageHandlers: [{Handler: newConsoleMessageHandler(),LogLevel: LogLevel.Critical|LogLevel.Debug,LogLevelIsBitMask: true}]});import{MessageHandlerBase,LogLevel,LoggerBuilder,LoggerConfigurationBuilder}from"simplr-logger";classMyMessageHandlerextendsMessageHandlerBase{publicHandleMessage(level: LogLevel,timestamp: number,messages: any[]): void{console.log(...messages);}}constconfig=newLoggerConfigurationBuilder().AddWriteMessageHandler({Handler: newMyMessageHandler(),LogLevel: LogLevel.Trace}).Build();constlogger=newLoggerBuilder(config);logger.Critical("Critical","message");logger.Debug("Debug","message");logger.Error("Error","message");logger.Info("Info","message");logger.Warn("Warn","message");logger.Trace("message","with trace");logger.Log(LogLevel.Information,"Info message");logger.Log(LogLevel.Critical,newError("Critical error"));// Using old configurationlogger.UpdateConfiguration(builder=>builder.SetPrefix("[new prefix]").Build());// Or with default configurationlogger.UpdateConfiguration(builder=>builder.SetPrefix("[new prefix]").Build(),false);| Name | Value | Description |
|---|---|---|
| None | 0 | Not used for writing log messages. Specifies that logger should not write any messages. |
| Critical | 1 | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. |
| Error | 2 | Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure. |
| Warning | 4 | Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop. |
| Information | 8 | Logs that track the general flow of the application. These logs should have long-term value. |
| Debug | 16 | Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value. |
| Trace | 32 | Logs that contain the most detailed messages. These messages may contain sensitive application data. These messages are disabled by default and should never be enabled in a production environment. |
| Name | Default value | Description |
|---|---|---|
| WriteMessageHandlers | [{ Handler: new ConsoleMessageHandler() }] | Message handlers list. ⁽¹⁾ |
| DefaultLogLevel | { LogLevel: LogLevel.Warning, LogLevelIsBitMask: false } | Log level or log levels in bit mask value. |
| Prefix | undefined | Custom message, which will be injected into the start of messages. |
(1) - The default value is only available if configuration property is not set.
Released under the MIT license.