2.0.7 • Published 9 months ago

ts-logger-node v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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.

2.0.7

9 months ago

2.0.6

9 months ago

2.0.5

9 months ago

2.0.4

9 months ago

2.0.3

9 months ago

2.0.2

9 months ago

2.0.0

9 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.0.25

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago