Ololog!
- Platform-agnostic logging
- Colors / styles for terminals and Chrome DevTools (try online demo)
- Displays call locations
- Returns its argument (for easy debugging of functional expressions)
- Smart indentation / newline handling
- Powerful object printer
- Formats
Error
instances as pretty stacktraces with source lines - Full sourcemap support (via
get-source
) - Pluggable pipeline architecture
- Can replace the default unhandled error printer in Node
- Integrates with Mocha (experimental)
Examples (NEW!)
These examples demonstrate some non-trivial complex behaviors that could be achieved with Ololog by plugging into it's rendering pipeline. For simpler examples read further docs!
-
Logging to a file and on screen at the same time (with different log levels)
-
Collapsing repeated messages (with an incrementing counter)
-
Displaying log levels and custom tags
TODO
Importing
For use with Node or with module bundlers (Browserify / WebPack / Rollup):
npm install ololog
const log =
Mocha
Using With
mocha --reporter ololog/reporter
- Aligns log messages nicely
- Supresses log output for nonfailed tests (disable with
.only
orthis.verbose=true
for a suite/test) - Automatically manages empty lines / whitespace for better legibility
- Prints unhandled exceptions and promise rejections as nice stacktraces
- Animated execution progress
NOTE: It is highly experimental yet, and things may not work as expected...
Browser Bundle
...for those who still uses <script>
tag for module importing ;) Exposes the global ololog
and ansicolor
objects. Installs String extensions for ANSI styles. Not compressed.
<!-- from unpkg.com CDN -->
Basic Usage
At first, it's similar to console.log
:
// foo bar baz
Configuration
It exposes a method called .configure
, which produces a new log
instance with the new settings applied (not mutating the original one), which can be saved and re-used subsequently:
const log =
// foobarbaz
...or you can apply the configuration method ad-hoc:
log 'foo' 'bar' 'baz' // foobarbaz
And you can chain the configuration calls, applying them subsequently:
log1 = log // removes the code location tag log2 = log1 // preserves previous settings + enables timestamps
The variety of possible options will be covered just below — there is a plenty of them!
Configuration engine is implemented as a separate external library, for everyone's use — you can read more about it here. Contributions are welcome.
Debugging Of Functional Expressions
Ololog returns its first argument (a feature that console.log
doesn't have), and it greatly simplifies debugging of functional expressions, as you can simply wrap part of an expression to log
:
array
It is far less ugly than with console.log
:
array
Also, if you don't like that behavior, you can override it. For example, returning the last argument instead of first:
log = log
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
methods on arbitrary strings:
nice // importing in .nice mode extends the String prototype, but there's a safe functional mode as well (see the docs...)
...or by using the built-in shorthand methods (no need to import ansicolor
, but we lose the ability to colorize just a part of a string):
loglogbrightred
See all the supported styling options here.
Smart Indentation / Newline Handling
To add indentation to a multiline text or complex objects, you can simply provide the indentation symbols as a first argument:
foo
bar
bar
The remarkable thing is that you can provide any text that would be used to offset what's coming after it. This is especially useful with printing long objects that span across many lines:
This is my object: { foo: 10,
bar: 20,
qux: 30 }
Compare it to the crappy console.log
output, which doesn't care about readability:
This is my object: { foo: 10,
bar: 20,
qux: 30 }
Ololog also handles the ANSI escape codes correctly while computing the proper the indentation width:
const bright = log

indent
Option
Using The The other way is to use the indent
config option:
log 'foo\nbar\nbaz\n'
Shorthand method:
log 'foo\n' 'bar\n' 'baz'
You can also set the indentation pattern should be used:
log = log
Smart Object Printing
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.
Default output:
// prints example object
{ asks: [ { price: "1000", amt: 10 },
{ price: "2000", amt: 10 } ],
bids: [ { price: "500", amt: 10 },
{ price: "100", amt: 10 } ] }
Longer strings:
logmaxLength 70 obj
{ asks: [{ price: "1000", amt: 10 }, { price: "2000", amt: 10 }],
bids: [{ price: "500", amt: 10 }, { price: "100", amt: 10 }] }
Shorter strings:
logmaxLength 20 obj
{ asks: [ { price: "1000",
amt: 10 },
{ price: "2000",
amt: 10 } ],
bids: [ { price: "500",
amt: 10 },
{ price: "100",
amt: 10 } ] }
Disabling right keys alignment:
log
{ obj: [ { someLongPropertyName: 1,
propertyName: 2,
anotherProp: 4,
moreProps: 5 },
{ propertyName: { someVeryLongPropertyName: true,
qux: 6,
zap: "lol" } } ] }
Disabling fancy nesting:
log
{
obj: [
{
someLongPropertyName: 1,
propertyName: 2,
anotherProp: 4,
moreProps: 5
},
{
propertyName: {
someVeryLongPropertyName: true,
qux: 6,
zap: "lol"
}
}
]
}
No fancy nesting + setting indentation width to 2 spaces:
log yetAnotherObj
{
obj: [
{
propertyName: 2,
moreProps: 5
}
]
}
Single line mode:
log
{ asks: [{ price: "1000", amount: 10 }, { price: "2000", amount: 10 }], bids: [{ price: "500", amount: 10 }, { price: "100", amount: 10 }] }
Changing max print depth / max array length:
log obj // or log.configure ({ stringify: { maxDepth: 1, maxArrayLength: 100 } })
log // disables limiting
Setting floating-point output precision:
log foo: 123456789
foo: 12345
Passing other configuration options to string.ify
(read the its docs for more info):
log obj
Avoid Too Long Call Chains
Please not that in case of multiple configuration options it is preferable to do that:
log
...instead of:
logmaxLength 20noFancy
...because the latter generates too deep callstack which could disrupt the displaying of the call location tag along with the message! The problem is yet to be solved in future pipez
versions.
Using With Custom Stringifier
Replacing the default printer with q-i (as an example):
const log =
Error
Instances
Pretty Printing This feature is implemented in the StackTracey library. See it's docs for more (you can configure the path shortening / library calls skipping).
logbright // where `e` is an instance of Error
or (if you want the output go to stderr and supress the grey location badge):
logbrightrederror

Using As The Default Exception Printer In Node
processprocess
Or you can simply call the handleNodeErrors
helper when importing Ololog:
const log =
panic-overlay
See Also: You can improve the error reporting not only in Node projects, but also in browsers. See the panic-overlay
library which shares the same codebase with Ololog:

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.
Disabling:
log ...
...or:
log
Custom printer:
log ...
Displaying outer call location (upwards the stack), can be useful when implementing library code / wrappers:
log ...
Manually setting call location (see the StackTracey library, which serves the purpose):
log ...
Timestamping
Disabled by default. To enable (with default options):
log = log

Configure formatting:
log = log
Here is the correspondence between the format
option value and the related Date
method used for rendering:
format value |
Date method |
---|---|
"locale" |
.toLocaleString () |
"iso" |
.toISOString () |
"utc" |
.toUTCString () |
null |
.toString () |
Providing a custom printer:
log 'Lorem ipsum dolor sit amet\nconsectetur adipiscing elit..'
Backdating:
log ...
Specifying Additional Semantics (errors / warnings / info messages)
You can add the .error
call modifier, which tells Ololog to render with the console.error
instead of the console.log
:
log
logbrightred
Other console
methods are supported as well:
log
log
log
INFO
/ WARN
/ ERROR
/ DEBUG
Tags
Displaying The There is a tag
stage (disabled by default) that displays the log level:
const log = loglogloglog

Customized Tag Printer
You can completely override the tag
stage, introducing new parameters and behavior (a clusterId
in this example):
const bullet = // NB: these packages are part of Ololog, no need to install them separatelyconst cyan yellow red dim = const log =
log 'foo'loglog
The output:

You can also use Custom Methods feature to make it even more concise:
log = log log 'foo'log log
Limiting Max Argument Length
log '1234567890' 'abcdefgh' // 1234… abcd…
Getting The Rendered Text
The following will execute all stages before the 'render' (screen output) stage, returning its argument:
log foo: 42 // '{ foo: 42 }'
The other way of getting the text (for example, if you want to intercept it and output to your custom renderer, like Blessed) is to override the default render
step, see below...
Overriding The Default Behavior
You can provide a custom implementation for certain steps in the Ololog's pipeline. For example, you can replace the render
step to output the rendered text to somewhere else other than console.log
:
log = log
You can look up all the default steps you could replace here:
https://github.com/xpl/ololog/blob/master/ololog.js#L67
Injecting Custom Code Before/After Steps
You can also bind new code to the existing methods in an aspect-oriented programming style, executing it before, after or instead – and thus overriding the default behavior. See the pipez library, which provides all the fun — with its help you could build incredibly configurable things similar to Ololog easily.
For example, if you want to write .error
calls not just on screen, but to a separate file, you can do following (by injecting a custom hook after the render
call):
const ololog = ansi = fs = const log =
Here's a complete example on how to set up a file logging that supports different log levels:
Here's another trick that you could do by injecting a handler before the render
step (that would be +render
instead of render+
):
Adding Custom Helper Methods
You can add your own shorthand methods/properties (will add new properties globally for any instance of the ololog
, but this may change in future). An example, demonstrating how the actual indent
and red
chain-style helpers were implemented:
log
Null Device
Use .null
to obtain a reduced instance that does nothing apart from returning its first argument:
const devNull = lognull
devNullbright // simply returns 'this never shows'
Powered By
Projects That Use Ololog
- CCXT – a cryptocurrency trading library with 100+ exchanges.