Commit ffcee101 authored by Kelvin Fichter's avatar Kelvin Fichter

feat(cmn): make logLevel a default option

Updates BaseServiceV2 so that logLevel is a default option to any new
instantiation of BaseServiceV2. Avoids people having to include hacky
code to set the log level.
parent 710aba90
---
'@eth-optimism/common-ts': patch
---
Make logLevel a default option of BaseServiceV2
...@@ -11,7 +11,7 @@ import promBundle from 'express-prom-bundle' ...@@ -11,7 +11,7 @@ import promBundle from 'express-prom-bundle'
import bodyParser from 'body-parser' import bodyParser from 'body-parser'
import morgan from 'morgan' import morgan from 'morgan'
import { Logger } from '../common/logger' import { Logger, LogLevel } from '../common/logger'
import { Metric, Gauge, Counter } from './metrics' import { Metric, Gauge, Counter } from './metrics'
import { validators } from './validators' import { validators } from './validators'
...@@ -23,6 +23,7 @@ export type StandardOptions = { ...@@ -23,6 +23,7 @@ export type StandardOptions = {
loopIntervalMs?: number loopIntervalMs?: number
port?: number port?: number
hostname?: string hostname?: string
logLevel?: LogLevel
} }
export type OptionsSpec<TOptions extends Options> = { export type OptionsSpec<TOptions extends Options> = {
...@@ -155,6 +156,7 @@ export abstract class BaseServiceV2< ...@@ -155,6 +156,7 @@ export abstract class BaseServiceV2<
loopIntervalMs?: number loopIntervalMs?: number
port?: number port?: number
hostname?: string hostname?: string
logLevel?: LogLevel
} }
) { ) {
this.loop = params.loop !== undefined ? params.loop : true this.loop = params.loop !== undefined ? params.loop : true
...@@ -176,6 +178,11 @@ export abstract class BaseServiceV2< ...@@ -176,6 +178,11 @@ export abstract class BaseServiceV2<
desc: 'Hostname for the app server', desc: 'Hostname for the app server',
default: params.hostname || '0.0.0.0', default: params.hostname || '0.0.0.0',
}, },
logLevel: {
validator: validators.logLevel,
desc: 'Log level',
default: params.logLevel || 'debug',
},
} }
// Add default options to options spec. // Add default options to options spec.
...@@ -322,7 +329,10 @@ export abstract class BaseServiceV2< ...@@ -322,7 +329,10 @@ export abstract class BaseServiceV2<
// Set up everything else. // Set up everything else.
this.loopIntervalMs = this.options.loopIntervalMs this.loopIntervalMs = this.options.loopIntervalMs
this.logger = new Logger({ name: params.name }) this.logger = new Logger({
name: params.name,
level: this.options.logLevel,
})
this.healthy = true this.healthy = true
// Gracefully handle stop signals. // Gracefully handle stop signals.
......
...@@ -13,6 +13,8 @@ import { Provider } from '@ethersproject/abstract-provider' ...@@ -13,6 +13,8 @@ import { Provider } from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer' import { Signer } from '@ethersproject/abstract-signer'
import { ethers } from 'ethers' import { ethers } from 'ethers'
import { LogLevel, logLevels } from '../common'
const provider = makeValidator<Provider>((input) => { const provider = makeValidator<Provider>((input) => {
const parsed = url()._parse(input) const parsed = url()._parse(input)
return new ethers.providers.JsonRpcProvider(parsed) return new ethers.providers.JsonRpcProvider(parsed)
...@@ -39,6 +41,14 @@ const wallet = makeValidator<Signer>((input) => { ...@@ -39,6 +41,14 @@ const wallet = makeValidator<Signer>((input) => {
} }
}) })
const logLevel = makeValidator<LogLevel>((input) => {
if (!logLevels.includes(input as LogLevel)) {
throw new Error(`expected log level to be one of ${logLevels.join(', ')}`)
} else {
return input as LogLevel
}
})
export const validators = { export const validators = {
str, str,
bool, bool,
...@@ -52,4 +62,5 @@ export const validators = { ...@@ -52,4 +62,5 @@ export const validators = {
provider, provider,
jsonRpcProvider, jsonRpcProvider,
staticJsonRpcProvider, staticJsonRpcProvider,
logLevel,
} }
...@@ -3,7 +3,15 @@ import pinoms, { Streams } from 'pino-multi-stream' ...@@ -3,7 +3,15 @@ import pinoms, { Streams } from 'pino-multi-stream'
import { createWriteStream } from 'pino-sentry' import { createWriteStream } from 'pino-sentry'
import { NodeOptions } from '@sentry/node' import { NodeOptions } from '@sentry/node'
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' export const logLevels = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
] as const
export type LogLevel = typeof logLevels[number]
export interface LoggerOptions { export interface LoggerOptions {
name: string name: string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment