项目作者: simplyappdevs

项目描述 :
Logging helper for Javascript/Typescript (NPM Package)
高级语言: TypeScript
项目地址: git://github.com/simplyappdevs/logging-helper.git
创建时间: 2020-12-04T04:50:30Z
项目社区:https://github.com/simplyappdevs/logging-helper

开源协议:GNU General Public License v3.0

下载


Logging Helper Module

Module to standardize logging output.

Why?

I don’t like using console.log everywhere in my code. So this module allows me to standardize log statements with certain format.

Notable Features

  • Log levels
  • Output by log levels
  • Standardized log structure that includes (but not limited to) app name, module name, function name, and other information
  • High-order functions to wrap module name and function name

Log Entry

  1. export interface LogEntry {
  2. logType: LOGTYPES; // Log type
  3. appName: string; // Application name
  4. modName: string; // Module name
  5. fnName: string; // Function name
  6. entryTS: Date; // Log entry timestamp in UTC
  7. friendlyMsg: string; // Log friendly/brief message
  8. detailMsg?: string; // Log detail message (ex: StackTrace from Error)
  9. task?: string; // Step/task being executed
  10. durationIsMS?: number; // Duration taken in millisecond
  11. }
  12. export interface LogEntryWithDuration extends LogEntry {
  13. endTS: Date; // Log entry end timestamp in UTC
  14. durationIsMS?: number; // Duration taken in millisecond
  15. }

Loggers

Main logger

Logger that takes all parameters

Module logger

High-order logger on top of main logger to pin the module name

Function logger

High-order logger on top of module logger to pin the function name

Log Output Override

This logger helper module does not actually output the log entries. It expects a logger output to be assigned via setLoggerOutput() function available from the main logger interface.

  1. export type LoggerOutput = (entry: Readonly<LogEntry> | Readonly<LogEntryWithDuration>) => void;

Example

https://github.com/simplyappdevs/logging-helper-example

Reminder for ESM Application

npm exec command option

You will need to run your application with --es-module-specifier-resolution=node option.

Ex: "exec": "node --es-module-specifier-resolution=node ./dist/index.js" for your NPM script npm run exec.

Configure package.json

Set type to module "type": "module"

  1. {
  2. "name": "my-awesome-app",
  3. "version": "1.0.0",
  4. "description": "My Awesome App",
  5. "main": "index.js",
  6. "type": "module",
  7. "scripts": {
  8. }
  9. }

Configure tsconfig.json

Set module to one of ECMA script "module": "esnext" in compilerOptions section

  1. {
  2. "compilerOptions": {
  3. "module": "esnext",
  4. }
  5. }

Set module resolution to node "moduleResolution": "node" in compilerOptions section

  1. {
  2. "compilerOptions": {
  3. "moduleResolution": "node",
  4. }
  5. }

Brought to you by www.simplyappdevs.com (2021)