Skip to content

Commit

Permalink
feature: control log level via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
dhenkel92 committed Sep 29, 2024
1 parent 31c76cc commit d025fee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# Remove all line-comments and save this as .env to enable
# Do not commit the filled-out file to GitHub

# possible values: all, trace, debug, info, warn, error, fatal, off (default: info)
LOG_LEVEL=all

# possible values: [unset], json, breif, verbose (default: unset)
LOG_FORMAT=

# Wix API key for import
API_KEY=

Expand Down Expand Up @@ -76,4 +82,4 @@ GAMIFICATION_ACTIVE=
REQUIRE_PUPIL_SCREENING=

# Credential for the OpenAI API to use their LLMs
OPENAI_API_KEY=
OPENAI_API_KEY=
12 changes: 6 additions & 6 deletions common/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import tracer from './tracing';
import formats from 'dd-trace/ext/formats';
import { isCommandArg } from '../util/basic';
import { configure, addLayout, getLogger as getlog4jsLogger, Logger as Log4jsLogger } from 'log4js';
import { getCurrentTransaction } from '../session';
import { getServiceName, getHostname } from '../../utils/environment';
import { getServiceName, getHostname, getLogLevel, getLogFormat } from '../../utils/environment';

addLayout('json', function () {
return function (logEvent) {
Expand All @@ -25,12 +24,13 @@ addLayout('json', function () {
};
});

const logFormat = getLogFormat();
let appenders = ['out'];
if (process.env.LOG_FORMAT === 'json') {
if (logFormat === 'json') {
appenders = ['outJson'];
} else if (process.env.LOG_FORMAT === 'brief') {
} else if (logFormat === 'brief') {
appenders = ['outBrief'];
} else if (process.env.LOG_FORMAT === 'verbose') {
} else if (logFormat === 'verbose') {
appenders = ['outBrief', 'fileVerbose'];
}

Expand Down Expand Up @@ -83,7 +83,7 @@ configure({
categories: {
default: {
appenders,
level: isCommandArg('--debug') ? 'debug' : 'info',
level: getLogLevel('info'),
},
},

Expand Down
8 changes: 8 additions & 0 deletions utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ export function isGamificationFeatureActive(): boolean {
export function isWebflowSyncDryRun(): boolean {
return JSON.parse(process.env.WEBFLOW_SYNC_DRY_RUN || 'true');
}

export function getLogLevel(defaultLevel: string): string {
return process.env.LOG_LEVEL || defaultLevel;
}

export function getLogFormat(): string | null {
return process.env.LOG_FORMAT;
}

0 comments on commit d025fee

Please sign in to comment.