Skip to content

Commit

Permalink
fix for nonworking print override, #15
Browse files Browse the repository at this point in the history
  • Loading branch information
xpl committed Feb 22, 2020
1 parent 80963b5 commit 8d7519c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ log ({ foo: true, bar: 42 })

![pic](https://user-images.githubusercontent.com/1707/30799941-222a66a8-a1e7-11e7-89b5-4bed706c7840.png)

You can also override the arguments stringification stage completely (see more on [overriding default behavior](https://github.com/xpl/ololog#overriding-the-default-behavior)):

```javascript
log = require ('ololog').configure ({ stringify (args, cfg) { return args.map (x => myCustomStringifier (x, cfg)) } })

// ...providing additional configuration somewhere later...
log = log.configure ({ stringify: { /* this object will be passed down as `cfg` to myCustomStringifier */ }})
```

# Pretty Printing `Error` Instances

This feature is implemented in the [StackTracey](https://github.com/xpl/stacktracey#pretty-printing) library. See it's docs for more (you can configure the path shortening / library calls skipping).
Expand Down
2 changes: 1 addition & 1 deletion ololog.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const log = pipez ({

/* ------------------------------------------------------------------------ */

stringify: (args, cfg, print = stringify.configure (cfg)) => args.map (arg => (typeof arg === 'string') ? arg : print (arg)),
stringify: (args, cfg, print = cfg.print || stringify.configure (cfg)) => args.map (arg => (typeof arg === 'string') ? arg : print (arg)),

trim: (tokens, { max = undefined }) => !max ? tokens : tokens.map (t => stringify.limit (t, max)),

Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ describe ('Ololog', () => {
assert (() => log.error ('an error'), ['\u001b[22m\u001b[1m\u001b[31m ERROR\t\u001b[39m\u001b[22man error'], 'error')
})
}

it ('custom stringifier works', () => {

// specifying custom printer
let log = ololog.configure ({ locate: false, stringify: { print (x) { return 'foo!' } } })
assert (() => log (42), ['foo!'])

// overriding stringify stage completely
log = ololog.configure ({ locate: false, stringify (args, cfg) { return args.map (x => 'foo!') } })
assert (() => log (42, 24), ['foo! foo!'])
})
})


Expand Down

0 comments on commit 8d7519c

Please sign in to comment.