Skip to content

Commit

Permalink
add colours option to console transport
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Oct 23, 2021
1 parent 49af650 commit e7ba862
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
20 changes: 20 additions & 0 deletions docs/transports/built-in/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,32 @@ Types:

```ts
interface ConsoleTransportOptions {
colours?: {
[level: string]: string
},
format?: string | ((this: CompleteConsoleTransportOptions, log: Log) => string),
level?: string,
timestamp?: string | ((date: Date) => string)
}
```

### `colours`

??? summary "Default"
```js
colours: {
critical: '&!4&0',
debug: '&1',
error: '&4',
info: '&3',
notice: '&!6&0',
success: '&2',
warn: '&6'
}
```
Colours used by the default format function.


### `format`

??? summary "Default"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leekslazylogger",
"version": "4.1.2",
"version": "4.1.3",
"description": "An easy-to-use and lightweight logger for Node.js with colours, timestamps, and files.",
"main": "dist/",
"types": "types/index.d.ts",
Expand Down
20 changes: 10 additions & 10 deletions src/transports/console/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { short } from 'leeks.js';
import DTF from '@eartharoid/dtf';

const dtf = new DTF('en-GB');
const colours = {
critical: '&!4&0',
debug: '&1',
error: '&4',
info: '&3',
notice: '&!6&0',
success: '&2',
warn: '&6'
};
const defaults: CompleteConsoleTransportOptions = {
colours: {
critical: '&!4&0',
debug: '&1',
error: '&4',
info: '&3',
notice: '&!6&0',
success: '&2',
warn: '&6'
},
format: function (this: CompleteConsoleTransportOptions, log: Log): string {
const timestamp = typeof this.timestamp === 'function' ? this.timestamp(log.timestamp) : dtf.fill(this.timestamp, log.timestamp);
const colour = colours[log.level.name] ?? '';
const colour = this.colours[log.level.name] ?? '';
return short(`${colour}[${timestamp}] [${log.level.name.toUpperCase()}] ${log.namespace ? `(${log.namespace.toUpperCase()}) ` : ''}${log.content}`);
},
level: 'info',
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ export interface TransportOptions {
}

export interface ConsoleTransportOptions {
colours?: {
[level: string]: string
},
format?: string | ((this: CompleteConsoleTransportOptions, log: Log) => string),
level?: string,
timestamp?: string | ((date: Date) => string)
}

export interface CompleteConsoleTransportOptions {
colours: {
[level: string]: string
},
format: string | ((this: CompleteConsoleTransportOptions, log: Log) => string),
level: string,
timestamp: string | ((date: Date) => string)
Expand Down

0 comments on commit e7ba862

Please sign in to comment.