JSPM

  • Created
  • Published
  • Downloads 4796
  • Score
    100M100P100Q119646F
  • License MIT

Platform-agnostic logging / ANSI styles / shows code locations / pretty prints objects

Package Exports

  • ololog

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ololog) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Ololog! BETA

Platform-agnostic logging / colors / styles / call locations / pretty prints / pluggable architecture.

Importing

log = require ('ololog')

Basic usage

At first, it's similar to console.log:

log ('foo', 'bar', 'baz') // foo bar baz

Although, comparing to Chrome...

pic

...our tokenizer implementation is more robust, producing no unneeded word separators:

log ('foo\n', 'bar', 'baz') // foo
                            // bar baz

.configure (...)

It's a pure function that produces a new log instance, with new settings applied (not mutating the original one). You can save it for the later use:

log = require ('ololog').configure ({ concat: { separator: '' }})
log ('foo', 'bar', 'baz') // foobarbaz

...or apply it ad-hoc:

log.configure ({ concat: { separator: '' }}) ('foo', 'bar', 'baz') // foobarbaz

You can stack up multiple configure calls (although this example is rather far-fetched):

log.configure ({ concat: { separator: ', ' }})
   .configure ({ lines: { linebreak: '<br>' }}) ('foo<br>', 'bar', 'baz') // foo
                                                                          // bar, baz

ANSI styling

Backed by the ansicolor library. Colored output is supported for the terminal environment and for the Chrome DevTools console. On other platforms, ANSI codes are safely stripped from the output, so they don't mess up anything.

Apply styling by calling the ansicolor directly:

require ('ansicolor').nice // Importing

log (('foo'.dim.red + 'bar'.bgBrightCyan).underline)

...or by using the built-in shorthand methods (no need to import ansicolor, but we lose the ability to colorize a part of the string):

log.red ('red text')
log.bright.red.underline ('multiple styles combined')'

Smart object printing

let  obj = { abc: 42, defgh: true, qwertyiop: 333, zap: '123457', long: ['foo', 'bar', 'baz', 'qux', 'lol', 'yup'] }
log (obj)
{       abc:    42,
      defgh:    true,
  qwertyiop:    333,
        zap:   "123457",
       long: [ "foo",
               "bar",
               "baz",
               "qux",
               "lol",
               "yup"  ]  }

Disabling fancy formatting:

log.configure ({ stringify: { pretty: false } }) (obj)
{ abc: 42, defgh: true, qwertyiop: 333, zap: "123457", long: ["foo", "bar", "baz", "qux", "lol", "yup"] }

All magic is provided by the external String.ify library. Read the docs to see all the available configuration options. There are plenty of them! Contributions are welcome.

Displaying call location

Have you ever encountered a situation where you need to quickly find in the code the place where the logging is called, but it's not so easy to do? With call location tags it's really easy. And it's enabled by default.

log message

call

Disabling:

log.configure ({ locate: false }) (...)

Custom printer:

log.configure ({ locate: { print: ({ calleeShort, fileName, line }) => ... } }) (...)

Manually setting call location (see the StackTracey library, which serves the purpose):

log.configure ({ locate: { where: new StackTracey ().at (2) } }) (...)

Indentation

log.configure ({ indent: { level: 3 } }) ('foo\n', 'bar\n', 'baz')  //          foo
                                                                    //          bar
                                                                    //          baz

Shorthand method:

log.indent (2) ('foo\n', 'bar\n', 'baz')

Timestamping

Disabled by default. To enable:

log = log.configure ({ time: true })

With indentation:

log            ('Lorem ipsum dolor sit amet\nconsectetur adipiscing elit..\n')
log.indent (2) ('Lorem ipsum dolor sit amet\nconsectetur adipiscing elit..\n')

pic

With custom printer:

log.configure ({ time: { print: x => (String (x) + ' | ').bright.cyan }}) ('Lorem ipsum dolor sit amet\nconsectetur adipiscing elit..')

pic

Backdating:

log.configure ({ time: { when: new Date ('2017-02-27T12:45:19.951Z') }}) (...)

Custom methods / aspect-oriented code injection

You can add your own shorthand methods, and you can also bind new code to the existing methods, executing it before, after or instead. See the pipez library, which provides all the fun.

log.methods ({

    indent (level) { return this.configure ({ indent: { level: level }}) }

    get red ()    { return this.configure ({ 'concat+': lines => lines.map (ansicolor.red) }) } // executes it after the 'concat'
    get bright () { return this.configure ({ 'concat+': lines => lines.map (ansicolor.bright) }) }  
})
log.indent (2).configure ({ time: true }).red.bright ('this is bold red message, indented by 2 and supplied with timestamp')

Limiting max argument length

log.configure ({ trim: { max: 5 } }) ('1234567890', 'abcdefgh') // 1234… abcd…