Skip to content

Commit

Permalink
feat: expose the styler from the logger and logger service (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 authored Sep 23, 2024
2 parents 264bc26 + e940f93 commit aee65a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/strange-snakes-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ogma/nestjs-module': minor
'@ogma/logger': minor
---

The `@ogma/styler` instance is now exposed at the `.style` property of ogma
13 changes: 12 additions & 1 deletion packages/logger/src/logger/ogma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export class Ogma {
*/
public log = this.info;

/**
* The [`@ogma/styler`](https://ogma.jaymcdoniel.dev/en/styler)
* instance that the logger uses for custom coloring without needing
* to manage a new styler instance
*/
public get style() {
return this.styler;
}

constructor(options?: Partial<OgmaOptions>) {
if (options?.logLevel) {
options.logLevel = options.logLevel.toUpperCase() as keyof typeof LogLevel;
Expand Down Expand Up @@ -82,6 +91,7 @@ export class Ogma {
if (this.options.json) {
this.hostname &&= `"hostname":${this.asString(this.hostname)},`;
this.pid &&= `"pid":${process.pid},`;
this.options.stream.getColorDepth = () => 1;
}
this.cachedMasks = this.options?.masks
? new Map(this.options.masks.map((mask) => [mask, true]))
Expand All @@ -107,12 +117,13 @@ export class Ogma {
if (this.options.color) {
colorDepthVal = 4;
}
if (this.options.color === false) {
if (this.options.color === false || this.options.json) {
colorDepthVal = 1;
}
if (!colorDepthVal && this.options.stream !== process.stdout && process.stdout.getColorDepth) {
colorDepthVal = process.stdout.getColorDepth();
}
console.log({ colorDepthVal, json: this.options.json });
this.options.stream.getColorDepth = () => colorDepthVal ?? 1;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/logger/test/ogma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { style } from '@ogma/styler';
import { spy, Stub } from 'hanbi';
import { Socket } from 'net';
import { suite } from 'uvu';
import { is, match, not, ok } from 'uvu/assert';
import { equal, is, match, not, ok } from 'uvu/assert';

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

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

OgmaSuite.run();
11 changes: 10 additions & 1 deletion packages/nestjs-module/src/ogma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ export class OgmaService implements LoggerService {
return requestContext.requestId;
}

/**
* The [`@ogma/styler`](https://ogma.jaymcdoniel.dev/en/styler)
* instance that the logger uses for custom coloring without needing
* to manage a new styler instance
*/
public get style() {
return this.ogma.style;
}

/**
* Change the log level during runtime
* @param the new log level to use
Expand Down Expand Up @@ -177,7 +186,7 @@ export class OgmaService implements LoggerService {

private printMessage(
message: any,
levelString: Exclude<keyof Ogma, 'printMessage' | 'printError' | 'setLogLevel'>,
levelString: Exclude<keyof Ogma, 'printMessage' | 'printError' | 'setLogLevel' | 'style'>,
meta: OgmaServiceMeta = {},
): void {
meta.context = meta.context ?? this.context;
Expand Down

0 comments on commit aee65a4

Please sign in to comment.