Skip to content

Commit

Permalink
change son logging transports
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 15, 2024
1 parent 3989943 commit b58a491
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
LOG_LEVELS_STR,
configureCustomDBTransport,
GENERIC_EMOJIS,
isDevelopmentEnvironment
USE_DB_TRANSPORT
} from '../../utils/logging/Logger.js'
import { DATABASE_LOGGER } from '../../utils/logging/common.js'
import { validateObject } from '../core/utils/validateDdoHandler.js'
Expand Down Expand Up @@ -1013,7 +1013,7 @@ export class Database {
// add this DB transport too
// once we create a DB instance, the logger will be using this transport as well
// we cannot have this the other way around because of the dependencies cycle
if (!isDevelopmentEnvironment()) {
if (USE_DB_TRANSPORT) {
configureCustomDBTransport(this, DATABASE_LOGGER)
} else {
DATABASE_LOGGER.warn(
Expand Down
24 changes: 17 additions & 7 deletions src/utils/logging/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ let customDBTransport: CustomOceanNodesTransport = null
export const MAX_LOGGER_INSTANCES = 10
export const NUM_LOGGER_INSTANCES = INSTANCE_COUNT

// log locations
const USE_CONSOLE_TRANSPORT: boolean =
process.env.LOG_CONSOLE && process.env.LOG_CONSOLE !== 'false'
const USE_FILE_TRANSPORT: boolean =
process.env.LOG_FILES && process.env.LOG_FILES !== 'false'
// can be affected by configuration
export const USE_DB_TRANSPORT: boolean =
true || (process.env.LOG_DB && process.env.LOG_DB !== 'false')

// if not set, then gets default 'development' level & colors
export function isDevelopmentEnvironment(): boolean {
const env = process.env.NODE_ENV || 'development'
Expand Down Expand Up @@ -200,7 +209,7 @@ function getDefaultOptions(moduleName: string): winston.LoggerOptions {
level: getDefaultLevel(),
levels: LOG_LEVELS_NUM,
format,
transports: [buildCustomFileTransport(moduleName), defaultConsoleTransport],
transports: getDefaultLoggerTransports(moduleName),
exceptionHandlers: [
new winston.transports.File({ dirname: 'logs/', filename: EXCEPTIONS_HANDLER })
]
Expand Down Expand Up @@ -257,12 +266,13 @@ export function buildCustomFileTransport(
export function getDefaultLoggerTransports(
moduleOrComponentName: string
): winston.transport[] {
// always log to file
const transports: winston.transport[] = [
buildCustomFileTransport(moduleOrComponentName)
]
// only log to console if development
if (isDevelopmentEnvironment()) {
const transports: winston.transport[] = []
if (USE_FILE_TRANSPORT) {
// always log to file
transports.push(buildCustomFileTransport(moduleOrComponentName))
}

if (USE_CONSOLE_TRANSPORT) {
transports.push(defaultConsoleTransport)
}
return transports
Expand Down

0 comments on commit b58a491

Please sign in to comment.