Skip to content

Latest commit

 

History

History
154 lines (120 loc) · 4.12 KB

README.md

File metadata and controls

154 lines (120 loc) · 4.12 KB

mrlm-net/logz

Logging package following SysLog protocol - RFC 5424 written in Typescipt.

Package mrlm-net/logz
NPM name @mrlm/logz
NPM version NPM Version
Latest version GitHub Release
License GitHub License

Table of contents

Installation

I'm using YARN so examples will be using it, you can install this package via any Node Package Manager.

$ yarn add @mrlm/logz

Usage

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

Advanced Usage

Using Outputs

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

Interfaces

LogLevel

export enum LogLevel {
    EMERGENCY = 0,
    ALERT = 1,
    CRITICAL = 2,
    ERROR = 3,
    WARNING = 4,
    NOTICE = 5,
    INFO = 6,
    DEBUG = 7
}

LogOutput

export enum LogOutput {
    STRING = "string",
    JSON = "json"
}

LogOptions

export interface LogOptions {
    level?: LogLevel;
    format?: LogOutput;
    formatCallback?: (level: LogLevel, message: string, additionalInfo?: object) => string;
    outputs?: Array<(level: LogLevel, message: string) => void>;
    prefix?: string;
}

ILogger

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;
}

Contributing

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>