# @mojule/log-formatter

> Simple formatting for logging

Latest version **0.3.0** (published 2020-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mojule/log-formatter
pnpm add @mojule/log-formatter
yarn add @mojule/log-formatter
bun add @mojule/log-formatter
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2020-03-11 |
| First published | 2019-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 35.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nik Coughlin |
| Maintainers | andybell, nrkn |

## Links

- npm: https://www.npmjs.com/package/@mojule/log-formatter
- Repository: https://github.com/mojule/log-formatter
- Homepage: https://github.com/mojule/log-formatter#readme
- Issues: https://github.com/mojule/log-formatter/issues
- npm.io page: https://npm.io/package/@mojule/log-formatter

## Recent versions

- 0.3.0 (latest) — 2020-03-11
- 0.2.2 — 2019-10-31
- 0.2.1 — 2019-09-02
- 0.2.0 — 2019-08-08
- 0.1.0 — 2019-08-07

## README

# log-formatter

Simple formatting for logging

`npm install @mojule/log-formatter`

## usage

### default logger

```js
import { logger } from '@mojule/log-formatter'
```

or:

```js
const { logger } = require( '@mojule/log-formatter' )
```

```js
logger.time( 'My program' )

logger.trace( 'My program trace' )
logger.debug( 'Debug', { foo: { bar: { baz: [ 1, 2, 3 ] } } } )
logger.info( 'Hello, world!', 1, [ 1, 2, 3 ], 'etc' )

try {
  logger.warn( 'Uh oh' )

  throw Error( 'A bad thing happened' )
} catch( err ){
  logger.error( err )
} finally {
  logger.fatal( 'Exiting program because of the bad thing' )
  logger.time( 'My program' )
}
```

- `logger.trace`, `logger.debug`, `logger.time` go to console.debug
- `logger.info` goes to `console.info`
- `logger.warn` goes to `console.warn`
- `logger.error` and `logger.fatal` goes to `console.error`

Note that `trace` and `time` are handled internally for consistency of
formatting rather than using `console.trace` or `console.time`

```
time    2019-08-08T00:40:02.752Z        Start   My program
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
trace   2019-08-08T00:40:02.758Z        My program trace
────────────────────────────────────────────────────────────────────────────────
Trace:
    at Object.<anonymous> (log-formatter/examples/default.js:5:8)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
debug   2019-08-08T00:40:02.759Z        Debug
────────────────────────────────────────────────────────────────────────────────
{
  "foo": {
    "bar": {
      "baz": [
        1,
        2,
        3
      ]
    }
  }
}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
info    2019-08-08T00:40:02.760Z        Hello, world!   1       [ 1, 2, 3 ]     etc
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
warn    2019-08-08T00:40:02.760Z        Uh oh
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
error   2019-08-08T00:40:02.760Z        Error   A bad thing happened
────────────────────────────────────────────────────────────────────────────────
Error: A bad thing happened
    at Object.<anonymous> (log-formatter/examples/default.js:12:9)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
fatal   2019-08-08T00:40:02.761Z        Exiting program because of the bad thing
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
time    2019-08-08T00:40:02.761Z        End     My program      0s 9.330659ms
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

```

### simple file logger

`createLogger` allows you to provide functions to call for each of the logging
levels rather than using the defaults, which go to `stdout` and `stderr`

The functions you provide are called with a single string argument containing
the message

All functions are optional, if you don't provide them, the output will go
nowhere - you can use this to implement custom logging levels

```js
import { createLogger } from '@mojule/log-formatter'
import { appendFile } from 'fs'
```

or:

```js
const { createLogger } = require( '@mojule/log-formatter' )
const { appendFile } = require( 'fs' )
```

```js
const stdOut = './examples/stdout.txt'
const stdErr = './examples/stderr.txt'

const append = ( path, data ) => appendFile(
  path, data + '\n', 'utf8', err => {
    if( err ) console.error( err )
  }
)

const writeOut = content => append( stdOut, content )
const writeErr = content => append( stdErr, content )

const trace = writeOut
const debug = writeOut
const time = writeOut
const info = writeOut
const warn = writeErr
const error = writeErr
const fatal = writeErr

const options = {
  trace, debug, time, info, warn, error, fatal
}

const logger = createLogger( options )

logger.time( 'My program' )

logger.trace( 'My program trace' )
logger.debug( 'Debug', { foo: { bar: { baz: [ 1, 2, 3 ] } } } )
logger.info( 'Hello, world!', 1, [ 1, 2, 3 ], 'etc' )

try {
  logger.warn( 'Uh oh' )

  throw Error( 'A bad thing happened' )
} catch( err ){
  logger.error( err )
} finally {
  logger.fatal( 'Exiting program because of the bad thing' )
  logger.time( 'My program' )
}

```

---
_Source: https://npm.io/package/@mojule/log-formatter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
