From 4a28a703abe38a4e55813ceb1a6ab25b0b6a8f52 Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Fri, 20 Oct 2023 19:00:38 -0400 Subject: [PATCH 1/2] fix logging --- README.md | 51 ++++++++++++++++++++++++++++++++++ src/config.js | 7 +++-- src/test-fixtures/.env.testing | 9 +++--- src/utils/logger.js | 32 ++++++++++----------- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0225c82..c9980ba 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - [Sign a credential](#sign-a-credential) - [Learner Credential Wallet](#learner-credential-wallet) - [Versioning](#versioning) +- [Logging](#logging) - [Development](#development) - [Testing](#testing) - [Contribute](#contribute) @@ -69,6 +70,10 @@ There is a sample .env file provided called .env.example to help you get started | `PORT` | http port on which to run the express app | 4006 | no | | `ENABLE_HTTPS_FOR_DEV` | runs the dev server over https - ONLY FOR DEV - typically to allow CORS calls from a browser | false | no | | `TENANT_SEED_{TENANT_NAME}` | see [tenants](#tenants) section for instructions | no | no | +| `ERROR_LOG_FILE` | log file for all errors - see [Logging](#logging) | no | no | +| `LOG_ALL_FILE` | log file for everything - see [Logging](#logging) | no | no | +| `CONSOLE_LOG_LEVEL` | console log level - see [Logging](#logging) | silly | no | +| `LOG_LEVEL` | log level for application - see [Logging](#logging) | silly | no | ### Tenants @@ -351,6 +356,52 @@ To ensure you've got compatible versions of the services and the coordinator, th If you do ever want to work from the source code in the repository and build your own images, we've tagged the commits in Github that were used to build the corresponding Docker image. So a github tag of v0.1.0 coresponds to a docker image tag of 0.1.0 +## Logging + +We support the following log levels: + +``` + error: 0, + warn: 1, + info: 2, + http: 3, + verbose: 4, + debug: 5, + silly: 6 +``` + +Logging is configured with environment variables, as defined in the [Environment Variables](#environment-variables) section. + +By default, everything is logged to the console (log level `silly`). + +You may set the log level for the application as whole, e.g., + +```LOG_LEVEL=http``` + +Which would only log messages with severity 'http' and all below it (info, warn, error). + +The default is to log everything (level 'silly'). + +You can also set the log level for console logging, e.g., + +```CONSOLE_LOG_LEVEL=debug``` + +This would log everything for severity 'debug' and lower (i.e., verbose, http, info, warn, error). This of course assumes that you've set the log level for the application as a whole to at least the same level. + +The default log level for the console is 'silly', which logs everything. + +There are also two log files that can be enabled: + +* errors (only logs errors) +* all (logs everything - all log levels) + +Enable each log by setting an env variable for each, indicating the path to the appropriate file, like this example: + +``` +LOG_ALL_FILE=logs/all.log +ERROR_LOG_FILE=logs/error.log +``` + ## Development ### Installation diff --git a/src/config.js b/src/config.js index 395b6b5..80a616c 100644 --- a/src/config.js +++ b/src/config.js @@ -2,6 +2,8 @@ import { generateSecretKeySeed, decodeSecretKeySeed } from '@digitalcredentials/ let CONFIG; const defaultPort = 4006 +const defaultConsoleLogLevel = 'silly' +const defaultLogLevel = 'silly' const testSeed = "z1AeiPT496wWmo9BG2QYXeTusgFSZPNG3T9wNeTtjrQ3rCB" const testTenantName = "test" const randomTenantName = "random" @@ -35,9 +37,8 @@ function parseConfig() { const env = process.env const config = Object.freeze({ enableHttpsForDev: env.ENABLE_HTTPS_FOR_DEV?.toLowerCase() === 'true', - enableAccessLogging: env.ENABLE_ACCESS_LOGGING?.toLowerCase() === 'true', - logToConsole: env.LOG_TO_CONSOLE?.toLowerCase() === 'true', - httpAccessLogFile: env.HTTP_ACCESS_LOG_FILE, + consoleLogLevel: env.CONSOLE_LOG_LEVEL?.toLocaleLowerCase() || defaultConsoleLogLevel, + logLevel: env.LOG_LEVEL?.toLocaleLowerCase() || defaultLogLevel, errorLogFile: env.ERROR_LOG_FILE, logAllFile: env.LOG_ALL_FILE, port: env.PORT ? parseInt(env.PORT) : defaultPort, diff --git a/src/test-fixtures/.env.testing b/src/test-fixtures/.env.testing index 23f1e09..8f9e72a 100644 --- a/src/test-fixtures/.env.testing +++ b/src/test-fixtures/.env.testing @@ -1,11 +1,10 @@ PORT=4007 -ENABLE_HTTPS_FOR_DEV=false - -ENABLE_ACCESS_LOGGING=true # default is false +ENABLE_HTTPS_FOR_DEV=false + LOG_ALL_FILE=logs/all.log -HTTP_ACCESS_LOG_FILE=logs/access.log ERROR_LOG_FILE=logs/error.log -LOG_TO_CONSOLE=true # default is true +CONSOLE_LOG_LEVEL=silly # default is silly, i.e. log everything - see the README for allowed levels +LOG_LEVEL=silly # default is silly TENANT_SEED_TESTING=z1AeiPT496wWmo9BG2QYXeTusgFSZPNG3T9wNeTtjrQ3rCB TENANT_SEED_TESTING2=z1AeiPT496wWmo9BG2QYXeTusgFSZPNG3T9wNeTtjrQ3rCC diff --git a/src/utils/logger.js b/src/utils/logger.js index 8c143ec..02a23af 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -1,7 +1,7 @@ import winston from 'winston'; import { getConfig } from '../config.js' -const { errorLogFile, httpAccessLogFile, logAllFile, logToConsole } = getConfig() +const { errorLogFile, logAllFile, logLevel, consoleLogLevel } = getConfig() /* These are the default npm logging levels that Winston uses, but we include them explicitly @@ -17,20 +17,23 @@ const levels = { silly: 6 } -// set severity based on NODE_ENV -// development: debug, i.e, log everything +// Set severity using LOG_LEVEL from env. +// If LOG_LEVEL is not set then set +// it using NODE_ENV from env, where: +// development: silly, i.e, log everything // production: warn and error const level = () => { - const env = process.env.NODE_ENV || 'development' - const isDevelopment = env === 'development' - return isDevelopment ? 'debug' : 'warn' + if (logLevel) { + return logLevel + } else { + const env = process.env.NODE_ENV || 'development' + const isDevelopment = env === 'development' + return isDevelopment ? 'silly' : 'warn' + } } const format = winston.format.combine( - - // add a timestamp winston.format.timestamp(), - // format all as json winston.format.json() ) @@ -39,7 +42,9 @@ Here we output as defined in the env */ const transports = [] - if (logToConsole) { transports.push(new winston.transports.Console())} + if (consoleLogLevel.toLowerCase() !== 'none') { transports.push(new winston.transports.Console({ + level: consoleLogLevel + }))} if (errorLogFile) { transports.push(new winston.transports.File({ @@ -48,13 +53,6 @@ const transports = [] })) } - if (httpAccessLogFile) { - transports.push(new winston.transports.File({ - filename: httpAccessLogFile, - level: 'http', - })) - } - if (logAllFile) { transports.push(new winston.transports.File({ filename: logAllFile From 2d46bfde04bf5c4ea111c47945181df786863ccc Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Fri, 20 Oct 2023 19:03:09 -0400 Subject: [PATCH 2/2] add env logging example --- .env.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.env.example b/.env.example index 055adc8..f8e6cab 100644 --- a/.env.example +++ b/.env.example @@ -14,3 +14,9 @@ ENABLE_HTTPS_FOR_DEV=false # ONLY for dev when need https; default is false TENANT_SEED_TESTING=z1AoLPRWHSKasPH1unbY1A6ZFF2Pdzzp7D2CkpK6YYYdKTN TENANT_SEED_RANDOMTESTING=generate TENANT_SEED_DEFAULT=generate + +# see the README for an explanation of logging +LOG_ALL_FILE=logs/all.log +ERROR_LOG_FILE=logs/error.log +CONSOLE_LOG_LEVEL=silly # default is silly, i.e. log everything - see the README for allowed levels +LOG_LEVEL=silly # default is silly \ No newline at end of file