Skip to content

Commit cfb7adc

Browse files
committed
docs(README): Update example code and add example outputs
1 parent 5131bba commit cfb7adc

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

README.md

+30-20
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
We wanted to have a unified, consistent mechanism for emitting log event data across Javascript/Typescript projects. We wanted to decouple log event emission from log event consumption. We wanted something lightweight.
1414

15-
For example, `encoda` and `dockta` are two Typescript projects that use `logga` which we combine in the `stencila` command line tool. In the future, we'll also combine them into the `stencila` desktop Electron-based application. We want users of `encoda` and `dockta` to be able to use them as standalone tools, at the command line, and get feedback at the command line. But we also want to be able to handle log events from these packages in apps that integrate them and which may use a fancier CLI display (e.g. [Ink](https://github.com/vadimdemedes/ink)) or a HTML UI display (e.g. in Electron).
15+
For example, `encoda` and `dockta` are two Typescript projects that use `logga`. We want users of these projects to be able to use them as standalone tools and have log events printed at the command line. Both projects are also integrated into the `stencila` command line tool, and in the future, we'll also combine them into the `stencila` desktop Electron-based application. For each of these apps, we want to handle log events from both packages in a consistent way and display them in a way that is appropriate for the platform e.g. HTML messages when in Electron, log files when running as a server.
1616

1717
## Approach
1818

@@ -26,31 +26,41 @@ npm install --save @stencila/logga
2626

2727
## Usage
2828

29-
Create a new logger by calling `getLogger` with a unique tag to identify your app and/or module. Then emit log events using the `debug`, `info`, `warn` or `error` function (you can pass them a message string or a `LogInfo` object).
29+
Create a new logger by calling `getLogger` with a unique tag to identify your app and/or module. Then emit log events using the `debug`, `info`, `warn` and `error` functions. You can pass them a message string or a `LogInfo` object.
3030

31-
```ts
32-
import { getLogger } from '@stencila/logga'
31+
```js
32+
const { getLogger } = require('@stencila/logga')
3333

34-
const logger = getLogger('app:module')
34+
const log = getLogger('example')
3535

36-
function doSomething() {
37-
logger.debug('A debug message')
36+
log.debug('This is line five.')
37+
log.info('Everything is just fine.')
38+
log.warn('Oh, oh, not no much.')
39+
log.error('Aaargh, an error!')
3840

39-
try {
40-
// ...
41-
} catch (error) {
42-
const { message, stack } = error
43-
logger.error({
44-
message: 'Woaaah something bad happened! ' + message,
45-
stack
46-
})
47-
}
41+
try {
42+
throw new Error('I am an error object.')
43+
} catch (error) {
44+
const { message, stack } = error
45+
log.error({
46+
message: 'Woaaah something bad happened! ' + message,
47+
stack
48+
})
4849
}
4950
```
5051

51-
The default log handler prints log data to `stderr`:
52+
The default log handler prints log data to `stderr`. If `stderr` is TTY log data is formatted for human consumption with emoji, colours and stack trace (for errors):
5253

53-
- with emoji, colours and stack trace (for errors) if `stderr` is TTY (for human consumption)
54-
- as JSON, with a time stamp, if `stderr` is not TTY (for machine consumption e.g. log files)
54+
![](screenshot.png)
5555

56-
You can register a new handler by calling `addHandler` with a handling function. Or use `replaceHandlers` to replace any existing log handlers (including the default) with your custom handler.
56+
If `stderr` is not TTY log data os formatted for machine consumption (e.g. for log files) as [ndjson](http://ndjson.org/), with a time stamp, if `stderr` (for machine consumption e.g. log files):
57+
58+
```json
59+
{"time":"2019-07-02T21:19:24.872Z","tag":"example","level":3,"message":"This is line five.","stack":"Error\n at Object.<anonymous> (/home/nokome/stencila/source/logga/example.js:21:5)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n at Module.load (internal/modules/cjs/loader.js:599:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:538:12)\n at Function.Module._load (internal/modules/cjs/loader.js:530:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)\n at startup (internal/bootstrap/node.js:266:19)"}
60+
{"time":"2019-07-02T21:19:24.875Z","tag":"example","level":2,"message":"Everything is just fine.","stack":"Error\n at Object.<anonymous> (/home/nokome/stencila/source/logga/example.js:22:5)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n at Module.load (internal/modules/cjs/loader.js:599:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:538:12)\n at Function.Module._load (internal/modules/cjs/loader.js:530:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)\n at startup (internal/bootstrap/node.js:266:19)"}
61+
{"time":"2019-07-02T21:19:24.875Z","tag":"example","level":1,"message":"Oh, oh, not no much.","stack":"Error\n at Object.<anonymous> (/home/nokome/stencila/source/logga/example.js:23:5)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n at Module.load (internal/modules/cjs/loader.js:599:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:538:12)\n at Function.Module._load (internal/modules/cjs/loader.js:530:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)\n at startup (internal/bootstrap/node.js:266:19)"}
62+
{"time":"2019-07-02T21:19:24.875Z","tag":"example","level":0,"message":"Aaargh, an error!","stack":"Error\n at Object.<anonymous> (/home/nokome/stencila/source/logga/example.js:24:5)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n at Module.load (internal/modules/cjs/loader.js:599:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:538:12)\n at Function.Module._load (internal/modules/cjs/loader.js:530:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)\n at startup (internal/bootstrap/node.js:266:19)"}
63+
{"time":"2019-07-02T21:19:24.875Z","tag":"example","level":0,"message":"Woaaah something bad happened! I am an error object.","stack":"Error: I am an error object.\n at Object.<anonymous> (/home/nokome/stencila/source/logga/example.js:27:9)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n at Module.load (internal/modules/cjs/loader.js:599:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:538:12)\n at Function.Module._load (internal/modules/cjs/loader.js:530:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)\n at startup (internal/bootstrap/node.js:266:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)"}
64+
```
65+
66+
You can register a new handler by calling `addHandler` with a handling function. Or use `replaceHandlers` to replace any existing log handlers (including the default).

example.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Example usage to generate example outputs.
3+
*
4+
* For pretty output run using,
5+
*
6+
* ```bash
7+
* node example.js
8+
* ```
9+
*
10+
* For machine readable output run using,
11+
*
12+
* ```bash
13+
* node example.js 2> log.json
14+
* ```
15+
*/
16+
17+
const { getLogger } = require('./dist')
18+
19+
const log = getLogger('example')
20+
21+
log.debug('This is line five.')
22+
log.info('Everything is just fine.')
23+
log.warn('Oh, oh, not no much.')
24+
log.error('Aaargh, an error!')
25+
26+
try {
27+
throw new Error('I am an error object.')
28+
} catch (error) {
29+
const { message, stack } = error
30+
log.error({
31+
message: 'Woaaah something bad happened! ' + message,
32+
stack
33+
})
34+
}

screenshot.png

128 KB
Loading

0 commit comments

Comments
 (0)