Giter VIP home page Giter VIP logo

logzen's Introduction

LogZen

Logzen is a simple and flexible logging library for Javascript that provides easy-to-use logging functionality with customizable log levels and the ability to output logs to different streams and consoles.

Documentation

Installation

You can install Logzen using npm:

npm install logzen

Usage

To use Logzen in your project, you need to import the Logger class from the Logzen package:

import { Logger } from 'logzen';

Basic Logging

Create a new Logger instance to start logging:

const logger = new Logger();

logger.log('This is a log message.');
logger.warn('This is a warning message.');
logger.error('This is an error message.');
logger.debug('This is a debug message.');

Customizing Log Levels

You can customize the log levels by setting the attachGlobalConsole option to false and manually attaching the console with custom log levels:

import { Logger, LogLevel } from 'logzen';
const logger = new Logger({ attachGlobalConsole: false });

// Attach console with custom log levels
logger.attach(console, LogLevel.WARN, LogLevel.ERROR);

logger.error('This message will be sent to the console.');
logger.log('This message will not be sent to the console!');

Logging to Files

Logzen supports logging to files by attaching file streams to the Logger instance. This allows you to send log entries with specific log levels to separate log files.

import { Logger, LogLevel } from 'logzen';
import fs from 'fs';

// Create a writable stream to a log file
const logFileStream = fs.createWriteStream('app.log', { flags: 'a' });
const errorFileStream = fs.createWriteStream('error.log', { flags: 'a' });

// Create the Logger instance
const logger = new Logger({ attachGlobalConsole: false });

// Attach the file stream with specific log levels
logger.attach(logFileStream, LogLevel.LOG, LogLevel.INFO);
logger.attach(errorFileStream, LogLevel.WARN, LogLevel.ERROR);

logger.log('This log message will be written to the app.log file.');
logger.error('This error message will also be written to the error.log file.');

Detaching Streams and Consoles

You can detach streams and consoles from the Logger using the detachStream and detachConsole methods:

const logger = new Logger();

// Detach streams and Console objects
logger.detach(stream, LogLevel.LOG, LogLevel.WARN);
logger.detach(console);

Attaching Consoles

You can also attach Console objects to the Logger instance to output logs to the console. This is useful if you have multiple consoles and want to output to all of them.

const logger = new Logger({ attachGlobalConsole: false });

logger.attach(console, LogLevel.WARN, LogLevel.ERROR);

logger.log('This log message will be not sent to the console.');
logger.error('This error will be sent to the console!');

Retaining Logs

By default, logs are retained in memory. You can disable log retention by setting the retainLogs option to false. Note that if you disable log retention, calling a Logger's toString method will return an empty string.

const logger = new Logger({ retainLogs: false });

logger.log('This log message will not be retained in memory.');
logger.warn('This warning message will also not be retained.');

Default log levels for attaching and detaching

When attaching a stream or console without specifying log levels, all will be attached. Likewise, when detaching, the stream or console will be completly removed.

FAQs

None yet!

logzen's People

Contributors

james-pre avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.