Git Product home page Git Product logo

js-logger's Introduction

Javascript jsLogger

Simple logger class to:

  • Keep track of log entries.
  • Keep track of timings with time() and timeEnd() methods.
  • Optionally write log entries in real-time to file (TODO) or to screen (DONE).
  • Optionally dump the log to file (TODO) or to string (DONE) in one go at any time.

Log entries can be added with any of the following methods:

  • info( message )
  • debug( message )
  • warning( message )
  • error( message )

For example, the following code

log = new jsLogger();
log.info( "an informational message intended for the user, ex: program started" );
log.debug( "a diagnostic message intended for the developer, ex: variable value = false" );
log.warning( "a warning that something might go wrong, ex: variable not set, something bad might happen" );
log.error( "explain why the program is going to crash, ex: file not found, exiting" );

will print to console the following lines:

$> 2018-05-01T10:06:07.050 [INFO] : an informational message intended for the user, ex: program started
$> 2018-05-01T10:06:07.050 [DEBUG] : a diagnostic message intended for the developer, ex: variable value = false
$> 2018-05-01T10:06:07.050 [WARNING] : a warning that something might go wrong, ex: variable not set, something bad might happen
$> 2018-05-01T10:06:07.051 [ERROR] : explain why the program is going to crash, ex: file not found, exiting

To write the same output to file, call the constructor with the third argument as true (TODO):

log = new jsLogger( null, null, true );

Time tracking

You can log the time that it takes to execute a block of code by using the time() and timeEnd() methods. For example, the following code:

log = new jsLogger();
log.time( "A small loop" );
setTimeout( function() { log.timeEnd( "A small loop" ); }, 500 );
log.time( "A big loop" );
setTimeout( function() { log.timeEnd( "A big loop" ); }, 3000 );

will wait three seconds and a half, and print to console the following:

2018-05-01T10:34:08.022 [DEBUG] : 'A small loop' took 0.506 seconds
2018-05-01T10:34:10.522 [DEBUG] : 'A big loop' took 3.006 seconds

Exact timings will vary depending on where the code is run.

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.