Skip to content

Commit aee65a4

Browse files
authored
feat: expose the styler from the logger and logger service (#1826)
2 parents 264bc26 + e940f93 commit aee65a4

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

.changeset/strange-snakes-poke.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ogma/nestjs-module': minor
3+
'@ogma/logger': minor
4+
---
5+
6+
The `@ogma/styler` instance is now exposed at the `.style` property of ogma

packages/logger/src/logger/ogma.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ export class Ogma {
4949
*/
5050
public log = this.info;
5151

52+
/**
53+
* The [`@ogma/styler`](https://ogma.jaymcdoniel.dev/en/styler)
54+
* instance that the logger uses for custom coloring without needing
55+
* to manage a new styler instance
56+
*/
57+
public get style() {
58+
return this.styler;
59+
}
60+
5261
constructor(options?: Partial<OgmaOptions>) {
5362
if (options?.logLevel) {
5463
options.logLevel = options.logLevel.toUpperCase() as keyof typeof LogLevel;
@@ -82,6 +91,7 @@ export class Ogma {
8291
if (this.options.json) {
8392
this.hostname &&= `"hostname":${this.asString(this.hostname)},`;
8493
this.pid &&= `"pid":${process.pid},`;
94+
this.options.stream.getColorDepth = () => 1;
8595
}
8696
this.cachedMasks = this.options?.masks
8797
? new Map(this.options.masks.map((mask) => [mask, true]))
@@ -107,12 +117,13 @@ export class Ogma {
107117
if (this.options.color) {
108118
colorDepthVal = 4;
109119
}
110-
if (this.options.color === false) {
120+
if (this.options.color === false || this.options.json) {
111121
colorDepthVal = 1;
112122
}
113123
if (!colorDepthVal && this.options.stream !== process.stdout && process.stdout.getColorDepth) {
114124
colorDepthVal = process.stdout.getColorDepth();
115125
}
126+
console.log({ colorDepthVal, json: this.options.json });
116127
this.options.stream.getColorDepth = () => colorDepthVal ?? 1;
117128
}
118129

packages/logger/test/ogma.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { style } from '@ogma/styler';
33
import { spy, Stub } from 'hanbi';
44
import { Socket } from 'net';
55
import { suite } from 'uvu';
6-
import { is, match, not, ok } from 'uvu/assert';
6+
import { equal, is, match, not, ok } from 'uvu/assert';
77

88
import { Ogma, OgmaOptions } from '../src';
99

@@ -518,5 +518,10 @@ OgmaSuite('The mixin should be called if for every log JSON-mode', ({ writeSpy,
518518
expectCallToPass(2);
519519
writeSpy.calls.forEach((c) => console.log(c.args.join(' ')));
520520
});
521+
OgmaSuite.only('a .style with json mode should return a regular string', ({ ogmaFactory }) => {
522+
process.env.FORCE_COLOR = '';
523+
const ogma = ogmaFactory({ json: true });
524+
equal(ogma.style.blue.apply('Hello World!'), 'Hello World!');
525+
});
521526

522527
OgmaSuite.run();

packages/nestjs-module/src/ogma.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export class OgmaService implements LoggerService {
4242
return requestContext.requestId;
4343
}
4444

45+
/**
46+
* The [`@ogma/styler`](https://ogma.jaymcdoniel.dev/en/styler)
47+
* instance that the logger uses for custom coloring without needing
48+
* to manage a new styler instance
49+
*/
50+
public get style() {
51+
return this.ogma.style;
52+
}
53+
4554
/**
4655
* Change the log level during runtime
4756
* @param the new log level to use
@@ -177,7 +186,7 @@ export class OgmaService implements LoggerService {
177186

178187
private printMessage(
179188
message: any,
180-
levelString: Exclude<keyof Ogma, 'printMessage' | 'printError' | 'setLogLevel'>,
189+
levelString: Exclude<keyof Ogma, 'printMessage' | 'printError' | 'setLogLevel' | 'style'>,
181190
meta: OgmaServiceMeta = {},
182191
): void {
183192
meta.context = meta.context ?? this.context;

0 commit comments

Comments
 (0)