Skip to content

Commit

Permalink
Send log messages through centralized logger
Browse files Browse the repository at this point in the history
Log messages that need to be recorded are now sent through the
centralized logger.
  • Loading branch information
luismiramirez committed Feb 24, 2022
1 parent c0e2063 commit 8f745e6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/nodejs/.changesets/add-centralized-logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bump: "patch"
type: "add"
---

Log messages are now sent through a new centralized logger that writes to `/tmp/appsignal.log` by default.
A warning is printed to STDERR when the default or provided logPath is not accessible, and the Logger
automatically falls back to STDOUT. Use config option `log: stdout` to log AppSignal messages to STDOUT instead.
5 changes: 4 additions & 1 deletion packages/nodejs/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Metrics } from "./interfaces"
import { Instrumentation } from "./instrument"
import { httpPlugin, httpsPlugin } from "./instrumentation/http"
import { BaseClient } from "./client"
import * as pgPlugin from "./instrumentation/pg"
import * as redisPlugin from "./instrumentation/redis"
import * as gcProbe from "./probes/v8"
Expand Down Expand Up @@ -30,7 +31,9 @@ export function initCorePlugins(
try {
instrumentation.load(PLUGIN_NAME, instrument)
} catch (e) {
console.warn(`Failed to instrument "${PLUGIN_NAME}": ${e.message}`)
BaseClient.logger.warn(
`Failed to instrument "${PLUGIN_NAME}": ${e.message}`
)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BaseClient implements Client {
readonly VERSION = VERSION

config: Configuration
logger: Logger
readonly logger: Logger
extension: Extension
instrumentation: Instrumentation

Expand Down
3 changes: 2 additions & 1 deletion packages/nodejs/src/instrument.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Plugin, Tracer, Metrics } from "./interfaces"
import { BaseClient } from "./client"

import Hook from "require-in-the-middle"
import semver from "semver"
Expand Down Expand Up @@ -46,7 +47,7 @@ export class Instrumentation {
if (semver.satisfies(version, plugin.version)) {
return plugin.install()
} else {
console.warn(
BaseClient.logger.warn(
`Unable to instrument module ${name}, module version needs to satisfy version range ${plugin.version}`
)

Expand Down
6 changes: 5 additions & 1 deletion packages/nodejs/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export class Logger {
}

const logFormat = printf(({ level, message, timestamp }) => {
return `[${timestamp} (process) #${process.pid}][appsignal][${level}] ${message}`
if (type == "file") {
return `[${timestamp} (process) #${process.pid}][${level}] ${message}`
} else {
return `[${timestamp} (process) #${process.pid}][appsignal][${level}] ${message}`
}
})

this.logger = winston.createLogger({
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class BaseSpan implements Span {
try {
span.setSpanSampleData(this._ref, key, Data.generate(data))
} catch (e) {
console.error(
BaseClient.logger.error(
`Error generating data (${e.name}: ${e.message}) for '${JSON.stringify(
data
)}'`
Expand Down

0 comments on commit 8f745e6

Please sign in to comment.