Giter Club home page Giter Club logo

timer-logs's Introduction

Timer logs

A minimalist NodeJS library, written in TypeScript, for logging code execution time and errors.

Designed to work well with Google Cloud logging, but useful for any application.

Fully documented with JSDoc(in .d.ts) and examples. Includes full typescript support.

Installation

npm install timer-logs

Usage

import { Timer } from 'timer-logs'

const webservice = async () => {
    const timer = new Timer({filename: 'webservice.ts'})
    timer.start('operation 1')
    await sleep(1000) // perform operation 1
    timer.next('operation 2')
    await sleep(2000) // perform operation 2
    timer.next('operation 3')
    await sleep(3000) // perform operation 2
    timer.flush()
}

Output:

{
  "severity": "INFO",
  "message": "Timer: 6025ms",
  "filename": "webservice.ts",
  "operation 1": 1010,
  "operation 2": 2000,
  "operation 3": 3014
}

Config options

You can specify more options when constructing the Timer object to customise the log output.

const timer = new Timer({
    filename: 'webservice.ts',
    label: 'Demo timer',
    details: {id: 'c69adf0e7ff8fddf8a93'},
    severity: 'ERROR'
})
{
  "severity": "ERROR",
  "message": "Demo timer: 61ms",
  "filename": "webservice.ts",
  "Demo timer": 61,
  "id": "c69adf0e7ff8fddf8a93"
}

Available methods

The most important function is timer.flush() which prints the log to the console. It uses information saved from all the other function calls including the constructor. This function call should go at the end of the function you are profiling.

Other functions

Start a new timer

timer.start('label for timer')

Start another timer, after the first one. (Stops the most recently started timer, and begins a new one)

timer.next('the next label you want to time')

End the most recently started timer. Is automatically called when flush is called if the most recently started timer has not been ended already.

timer.end()

To stop a specific timer by name, there are two options

const timer1 = timer.start('timer 1')
const timer2 = timer.start('timer 2')
// perform operations
timer.stop('timer 1') // similar to console.time()
timer2.stop() //doesn't require the label

Error logging

Custom error messages

timer.customError('Zero length list returned. Expecting at least 1 item')

Output:

{
  "severity": "ERROR",
  "message": "Zero length list returned. Expecting at least 1 item",
  "filename": "webservice.ts"
}

Postgres error handling

const queryResult = await pool.query(`SELECT NOW()`).catch(e => timer.postgresError(e))

Generic error handling

try {
    JSON.parse('asdfghjkl;')
} catch (e) {
    timer.genericError(e)
}

Output

{
  "severity": "ERROR",
  "errorMessage": "Unexpected token a in JSON at position 0",
  "errorName": "SyntaxError",
  "stackTrace": "SyntaxError: Unexpected token a in JSON at position 0\n    at JSON.parse (<anonymous>)\n    at Object.<anonymous> (C:\\Users\\webservice.js:6:10)\n    at Module._compile (node:internal/modules/cjs/loader:1109:14)\n    atObject.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)\n    at Module.load (node:internal/modules/cjs/loader:989:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:829:14)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)\n    at node:internal/main/run_main_module:17:47",
  "filename": "webservice.ts"
}

timer-logs's People

Contributors

mrbrianevans avatar

Watchers

 avatar  avatar

timer-logs's Issues

Use performance API

Instead of measuring time with Date.now(), rather use the built in performance API which gives high resolution time: performance.now(). It works in node via perf_hooks and in browser from the window object. This will allow for more accurate timing

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.