Logging package following SysLog protocol - RFC 5424 written in Typescipt.
Package | mrlm-net/logz |
---|---|
NPM name | @mrlm/logz |
NPM version | |
Latest version | |
License |
I'm using
YARN
so examples will be using it, you can install this package via any Node Package Manager.
$ yarn add @mrlm/logz
import { Logger, LogLevel, LogOutput } from '@mrlm/logz';
const logger = new Logger({
level: LogLevel.DEBUG,
format: LogOutput.STRING,
prefix: 'my-app'
});
logger.info('This is an info message');
logger.error('This is an error message', { errorCode: 123 });
Output:
[my-app] [2023-10-05T14:48:00.000Z] [INFO] This is an info message
[my-app] [2023-10-05T14:48:00.000Z] [ERROR] ["123"] This is an error message
You can customize the logger to output logs to different destinations. For example, you can use the Console
object to stream logs to a file.
import { Logger, LogLevel, LogOutput } from '@mrlm/logz';
import { Console } from 'console';
import { createWriteStream } from 'fs';
const output = createWriteStream('./stdout.log');
const errorOutput = createWriteStream('./stderr.log');
const loggerConsole = new Console({ stdout: output, stderr: errorOutput });
const logger = new Logger({
level: LogLevel.DEBUG,
format: LogOutput.STRING,
outputs: [
(level, message) => {
if (level <= LogLevel.ERROR) {
loggerConsole.error(message);
} else {
loggerConsole.log(message);
}
}
],
prefix: 'my-app'
});
logger.info('This is an info message');
logger.error('This is an error message', { errorCode: 123 });
Output in stdout.log
:
[my-app] [2023-10-05T14:48:00.000Z] [INFO] This is an info message
Output in stderr.log
:
[my-app] [2023-10-05T14:48:00.000Z] [ERROR] ["123"] This is an error message
export enum LogLevel {
EMERGENCY = 0,
ALERT = 1,
CRITICAL = 2,
ERROR = 3,
WARNING = 4,
NOTICE = 5,
INFO = 6,
DEBUG = 7
}
export enum LogOutput {
STRING = "string",
JSON = "json"
}
export interface LogOptions {
level?: LogLevel;
format?: LogOutput;
formatCallback?: (level: LogLevel, message: string, additionalInfo?: object) => string;
outputs?: Array<(level: LogLevel, message: string) => void>;
prefix?: string;
}
export interface ILogger {
log(level: LogLevel, message: string, additionalInfo?: object): void;
emergency(message: string, additionalInfo?: object): void;
alert(message: string, additionalInfo?: object): void;
critical(message: string, additionalInfo?: object): void;
error(message: string, additionalInfo?: object): void;
warning(message: string, additionalInfo?: object): void;
notice(message: string, additionalInfo?: object): void;
info(message: string, additionalInfo?: object): void;
debug(message: string, additionalInfo?: object): void;
}
Contributions are welcomed and must follow Code of Conduct and common Contributions guidelines.
If you'd like to report security issue please follow security guidelines.
All rights reserved © Martin Hrášek <@marley-ma> and WANTED.solutions s.r.o. <@wanted-solutions>