Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: control log level via env vars #1146

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ The following configuration can be done via ENV variables:

| Name | Value | Description |
| ---------- | ------- | ------------------------------------------------------------------------- |
| LOG_LEVEL | all | Log levels are used to categorize log events by severity and control the |
| | trace | verbosity of the logs. Using levels, you can filter out less important |
| | debug | logs and focus on the most critical ones. |
| | info | |
| | warn | |
| | error | |
| ---------- | ------- | ------------------------------------------------------------------------- |
| LOG_FORMAT | (unset) | Every log prefixed by session and transaction id, also log HTTP requests |
| | json | Log as JSON, used in deployed environments to pass rich info to Datadog |
| | brief | Only log category and message (omitting session prefix and HTTP requests) |
Expand All @@ -105,9 +112,9 @@ The following configuration can be done via ENV variables:

The following command line arguments are available (i.e. run `npm run web -- --debug`):

| Argument | Description |
| -------- | ---------------------------------------------------------------- |
| \--debug | Sets the log level to debug which prints out tons of information |
| Argument | Description |
| -------- | ------------------------------------ |
| None | There are no cli arguments right now |

### Changes to the Data-Model

Expand Down
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;
}
Loading