Giter Club home page Giter Club logo

tslogger's Introduction

TS-Logger-Node

ts-logger-node is a Node.js logging package that provides a simple yet powerful logging mechanism with customizable log file paths and console output styles. This package is written in TypeScript and comes with type definitions for enhanced development experience.

Installation

To install ts-logger-node, you can use npm or yarn:

npm install ts-logger-node

or

yarn add ts-logger-node

Usage

Importing the Logger

To use the logger, import it into your project:

import Logger from "ts-logger-node";

Logging Messages

You can log messages with different types: ERROR, GENERAL, or HEADER. The print method is used to log messages, and it takes three arguments:

  • msg (string): The log message you want to write.
  • type (LogType): The log type, which can be "ERROR", "GENERAL", or "HEADER".
  • options (optional object): Additional options for styling the console output. It contains a style property (string) that accepts ANSI escape sequences for colors and other text styles.
Logger.print("This is an error message", "ERROR");
Logger.print("This is a general message", "GENERAL", {
  style: textStyles.FgGreen,
});
Logger.print("This is a header message", "HEADER", {
  style: textStyles.BgBlue,
});

Customizing Log File Paths

By default, the log files are stored in the "logs" directory with filenames "general.log" and "error.log". You can customize these file paths using the paths setter:

Logger.paths = {
  general: {
    dir: "custom-logs/",
    fileName: "custom-general",
    fileExt: ".log",
  },
  error: {
    dir: "custom-logs/",
    fileName: "custom-error",
    fileExt: ".log",
  },
};

Log Return Types

The print method returns a LogReturnType that indicates the status of the log operation. The possible return types are:

  • "SUCCESS_LOG": The log message was successfully written to the log file and console.
  • "ERROR_LOG": The log message was successfully written to the error log file, and the error message was printed to the console.
  • "FILE_WRITE_ERROR": There was an error writing the log message to the log file.

Available Text Styles

The package also exports a textStyles object, which provides a set of ANSI escape sequences for styling the console output. You can use these styles in the options.style property to customize the appearance of log messages.

import Logger, { textStyles } from " ts-logger-node";

Logger.print("This text will be blue!", "GENERAL", {
  style: textStyles.FgBlue,
});
Logger.print("White text on red background!", "GENERAL", {
  styles: {
    style: textStyles.BgRed + textStyles.FgWhite,
  },
});

Next Usability

Implimenting ts-logger-node can only be done in server-components currently. In order to use Logger, or any other package that requries 'fs', in a NextJS app the a next.config file must include the following:

const nextConfig = {
  webpack5: true,
  webpack: (config) => {
    config.resolve.fallback = { fs: false };

    return config;
  }
}

Contributing

If you find any issues with ts-logger-node or want to contribute improvements or new features, please feel free to open an issue or submit a pull request on the GitHub repository.

License

This package is licensed under the MIT License. See the .LICENSE file for details.

tslogger's People

Contributors

tylerswindell avatar github-actions[bot] avatar lritter79 avatar

Watchers

 avatar

Forkers

lritter79

tslogger's Issues

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.