Skip to content

Commit

Permalink
feat(observability): ensure logger can be configured in browser
Browse files Browse the repository at this point in the history
closes #430
  • Loading branch information
ygrishajev committed Nov 28, 2024
1 parent 7f6f9ca commit 9ac2fdb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
7 changes: 2 additions & 5 deletions packages/logging/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ dotenv.config();

const envSchema = z.object({
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
STD_OUT_LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json"),
FLUENTD_TAG: z.string().optional().default("pino"),
FLUENTD_HOST: z.string().optional(),
FLUENTD_PORT: z.number({ coerce: true }).optional().default(24224),
STD_OUT_LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json")
});

export const envConfig = envSchema.parse(process.env);
export const envConfig = envSchema.parse(process.env);
6 changes: 4 additions & 2 deletions packages/logging/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { envConfig } from "./env.config"
import { envConfig } from "./env.config";

export const config = {
...envConfig
}
};

export type Config = typeof config;
15 changes: 12 additions & 3 deletions packages/logging/src/servicies/logger/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import pino from "pino";
import { gcpLogOptions } from "pino-cloud-logging";
import type { PinoPretty } from "pino-pretty";

import { config } from "../../config";
import { Config, config as envConfig } from "../../config";

export type Logger = Pick<pino.Logger, "info" | "error" | "warn" | "debug">;

Expand All @@ -16,6 +16,15 @@ interface LoggerOptions extends pino.LoggerOptions {
}

export class LoggerService implements Logger {
static config: Config = envConfig;

static configure(config: Partial<Config>) {
this.config = {
...this.config,
...config
};
}

static forContext(context: string) {
return new LoggerService().setContext(context);
}
Expand All @@ -30,7 +39,7 @@ export class LoggerService implements Logger {

private initPino(): pino.Logger {
const options: LoggerOptions = {
level: config.LOG_LEVEL,
level: LoggerService.config.LOG_LEVEL,
mixin: LoggerService.mixin,
timestamp: () => `,"time":"${new Date().toISOString()}"`,
...this.options
Expand All @@ -46,7 +55,7 @@ export class LoggerService implements Logger {
}

private getPrettyIfPresent(): PinoPretty.PrettyStream | undefined {
if (typeof window !== "undefined" || config.STD_OUT_LOG_FORMAT !== "pretty") {
if (typeof window !== "undefined" || LoggerService.config.STD_OUT_LOG_FORMAT !== "pretty") {
return;
}

Expand Down

0 comments on commit 9ac2fdb

Please sign in to comment.