Git Product home page Git Product logo

tracing-log's Introduction

tracing-log

A simple tracing logger.

It will include FILE_NAME:LINE_NUMBER:FUNCTION_NAME in each log line.

npm install tracing-log

Usage:

Logging:


const tl = require('tracing-log')

// create a log object
const log = tl.gen_logger("some_identifier")


// use log functions:

log.error("some error msg")
log.warn("some warn msg")
log.info("some info msg")
log.debug("some debug msg")

Setting log level:

tl.level = tl.INFO

Setting a custom logging function:

tl.set_log_function((level, msg) => {
   console.log(`${m().format("HH:mm:ss.SSS")} ${tl.levels[level]} ${msg}`)
})

Sample usage

$ nl -b a test.js
     1
     2  const tl = require('./index.js')
     3  const m = require('moment')
     4
     5  console.log("------------------")
     6
     7  const log = tl.gen_logger("steam_engine")
     8
     9  log.error("<error> something bad happened")
    10
    11  function bla() {
    12      log.warn("<warn> inside bla: something bad happened")
    13      ble()
    14  }
    15
    16  function ble() {
    17      log.info("<info> inside ble: something bad happened")
    18      bli()
    19  }
    20
    21  function bli() {
    22      log.debug("<debug> inside bli: something bad happened")
    23  }
    24
    25  bla()
    26
    27  console.log("------------------")
    28
    29  tl.set_log_function((level, msg) => {
    30      console.log(`Some_blabla_prefix ${m().format("YYYY-MM-DD HH:mm:ss.SSS")} ${tl.levels[level]} ${msg}`)
    31  })
    32
    33  log.error("<error> something bad happened")
    34
    35  bla()
    36


$ node test.js 
------------------
2023-05-12 17:50:42.952 ERROR test.js:9:null steam_engine <error> something bad happened
2023-05-12 17:50:42.954 WARN test.js:12:bla steam_engine <warn> inside bla: something bad happened
2023-05-12 17:50:42.954 INFO test.js:17:ble steam_engine <info> inside ble: something bad happened
2023-05-12 17:50:42.955 DEBUG test.js:22:bli steam_engine <debug> inside bli: something bad happened
------------------
Some_blabla_prefix 2023-05-12 17:50:42.955 ERROR test.js:33:null steam_engine <error> something bad happened
Some_blabla_prefix 2023-05-12 17:50:42.955 WARN test.js:12:bla steam_engine <warn> inside bla: something bad happened
Some_blabla_prefix 2023-05-12 17:50:42.955 INFO test.js:17:ble steam_engine <info> inside ble: something bad happened
Some_blabla_prefix 2023-05-12 17:50:42.955 DEBUG test.js:22:bli steam_engine <debug> inside bli: something bad happened

tracing-log's People

Contributors

mayamatakeshi avatar

Watchers

 avatar  avatar

tracing-log's Issues

Permit to disable tracing

By default, gen_logger (see #1) should enable tracing.
But it should be possible to pass a bool to disable it.
This is because the stacktrace operations are expensive and we might want to disable them in production and only enable them during development/test/troubleshooting.

Create function to permit to generate a tracing-log object for a specific subject

Something llike this:

function gen_log(subject) {
  const newLog = Object.assign({}, tl)
  newLog.error = logger.error.bind(null, subject)
  newLogwarn = logger.warn.bind(null, subject)
  newLog.info = logger.info.bind(null, subject)
  newLog.debug = logger.debug.bind(null, subject)
  newLog.log = logger.log.bind(null, subject)
  return newLog
}

// Example usage:
const tl = require('tracing-log')
const log = tl.gen_log('some_uuid')
log.info('Hello, world!') // Output: 2023-05-01 12:34:56.789 INFO some_uuid FILE_NAME:LINE_NUMBER:FUNCTION_NAME Hello, world!

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.