Skip to content

Commit 4b90bed

Browse files
feat(console): Add gray ANSI color and use it for silly levels (#472)
* feat(core): Add `gray` ANSI color * feat(console): Use `gray` style for `verbose`+`debug`+`silly` levels * feat(console): Add log.transports.console.colorMap option --------- Co-authored-by: Alexey Prokhorov <[email protected]>
1 parent dc731b4 commit 4b90bed

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 5.4.2
4+
5+
- [#472](https://github.com/megahertz/electron-log/pull/472) Add the gray color
6+
and `log.transports.console.colorMap` option.
7+
38
## 5.4.0
49

510
- [#465](https://github.com/megahertz/electron-log/issues/465) Allow using `%c`

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Available colors:
196196
- magenta
197197
- cyan
198198
- white
199+
- gray
199200

200201
For DevTools console you can use other CSS properties.
201202

docs/transports/console.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ Displays a log message in the console
44

55
## Options
66

7+
#### `colorMap` {Record<LogLevel, string>}
8+
9+
Default:
10+
```
11+
{
12+
error: 'red',
13+
warn: 'yellow',
14+
info: 'cyan',
15+
verbose: 'unset',
16+
debug: 'gray',
17+
silly: 'gray',
18+
default: 'unset',
19+
}
20+
```
21+
22+
A map of log levels to colors.
23+
724
#### `format` {string | (message: LogMessage) => void}
825

926
Default: `'[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}'`

src/core/transforms/style.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ANSI_COLORS = {
2222
magenta: '\x1b[35m',
2323
cyan: '\x1b[36m',
2424
white: '\x1b[37m',
25+
gray: '\x1b[90m',
2526
};
2627

2728
function styleToAnsi(style) {

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ declare namespace Logger {
8282
}
8383

8484
interface ConsoleTransport extends Transport {
85+
/**
86+
* A mapping of log levels to their corresponding color name
87+
*/
88+
colorMap: Record<LogLevel, string>;
89+
8590
/**
8691
* String template of function for message serialization
8792
*/

src/node/transports/console.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ Object.assign(consoleTransportFactory, {
3434

3535
function consoleTransportFactory(logger) {
3636
return Object.assign(transport, {
37+
colorMap: {
38+
error: 'red',
39+
warn: 'yellow',
40+
info: 'cyan',
41+
verbose: 'unset',
42+
debug: 'gray',
43+
silly: 'gray',
44+
default: 'unset',
45+
},
3746
format: DEFAULT_FORMAT,
3847
level: 'silly',
3948
transforms: [
@@ -68,7 +77,11 @@ function addTemplateColors({ data, message, transport }) {
6877
return data;
6978
}
7079

71-
return [`color:${levelToStyle(message.level)}`, 'color:unset', ...data];
80+
return [
81+
`color:${levelToStyle(message.level, transport)}`,
82+
'color:unset',
83+
...data,
84+
];
7285
}
7386

7487
function canUseStyles(useStyleValue, level) {
@@ -88,7 +101,6 @@ function formatStyles(args) {
88101
return nextTransform(args);
89102
}
90103

91-
function levelToStyle(level) {
92-
const map = { error: 'red', warn: 'yellow', info: 'cyan', default: 'unset' };
93-
return map[level] || map.default;
104+
function levelToStyle(level, transport) {
105+
return transport.colorMap[level] || transport.colorMap.default;
94106
}

0 commit comments

Comments
 (0)