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

Jc fix logging #21

Merged
merged 2 commits into from
Oct 20, 2023
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
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions src/test-fixtures/.env.testing
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 15 additions & 17 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
)

Expand All @@ -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({
Expand All @@ -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
Expand Down
Loading