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

Problems with fetching logs from winston inside Nextjs 14 #1348

Open
not-aesthetic-code opened this issue Jun 15, 2024 · 6 comments
Open

Problems with fetching logs from winston inside Nextjs 14 #1348

not-aesthetic-code opened this issue Jun 15, 2024 · 6 comments

Comments

@not-aesthetic-code
Copy link

not-aesthetic-code commented Jun 15, 2024

Hey, I'm struggling with receiving logs from winston. Unfortunately any of logs is gathered. I know that appInsights is connected because I'm able to hit with the other telemetric requests.

Environment:
Nextjs 14.0.4
Node: 20.10.0

Execution
Whole app is runned inside docker container, where the final command to run the app is: node --require ./scripts/appInsights.js && next dev,

Config:
appInsights.js
`
appInsights
.setup(')
.setAutoCollectConsole(true, true) // tried (true, false) (true) also
.setAutoCollectDependencies(true)
.setAutoCollectExceptions(true)
.setAutoCollectHeartbeat(true)
.setAutoCollectPerformance(true, true)
.setAutoCollectRequests(true)
.setAutoDependencyCorrelation(true)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
.setSendLiveMetrics(true)
.setUseDiskRetryCaching(true)

appInsights.start()
`
Winston:

`import { Logger, createLogger, format, transports } from "winston"
const { json } = format

export const logger = (moduleName: string): Logger => {
const nameOfModule = moduleName.toUpperCase();
const loggingFormat = json();

const loggerObject = createLogger({
format: loggingFormat,
defaultMeta: { context: nameOfModule },
transports: [new transports.Console()],
})

return loggerObject
}`

winston usage

for example:
`import {logger} from '@services/logger'

export const apiLogger = logger("API")
apiLogger.error('Error fetching favicon:', e);`

I was able to artifically generate telemetric data right after appInsights.start() with these functions:
`function trackEvent() {
const client = appInsights.defaultClient;
const telemetry = { name: 'Custom Event V2', properties: { key: 'value' } };
client.trackEvent(telemetry);
client.flush();

console.log('Telemetry data sent - Event:', telemetry);
}

function trackTrace() {
const client = appInsights.defaultClient;
const message = 'This is a sample trace message.';
client.trackTrace({ message });
client.flush();

console.log('Telemetry data sent - Trace:', message);
}

function trackMetric() {
const client = appInsights.defaultClient;
const metricName = 'Custom Metric';
const value = Math.floor(Math.random() * 100);
client.trackMetric({ name: metricName, value });
client.flush();

console.log('Telemetry data sent - Metric:', metricName, value);
}

trackEvent();
trackTrace();
trackMetric();
`

But besides of that, no trace is visible on my app insights resource.

Nextjs info. I've tried to implement telemetry according this logs too by importing a script into register function mentioned in this doc
https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation also with no result.

Looking for feedback from you!

@JacksonWeber
Copy link
Contributor

@not-aesthetic-code are you seeing the winston logs from above printed in the console? Or is it not showing up in App Insights or there?

@not-aesthetic-code
Copy link
Author

not-aesthetic-code commented Jun 24, 2024

@JacksonWeber Yes, every log from winston was clearly visible from the console, but non of these logs was send to the app Insights.

@not-aesthetic-code
Copy link
Author

so @JacksonWeber Do we have any idea here?

@JacksonWeber
Copy link
Contributor

@not-aesthetic-code It looks like in your above example you're able to manually collect the other telemetry types. Are you able to auto-collect any telemetry outside of logs? (dependencies, requests, etc.)

@not-aesthetic-code
Copy link
Author

@JacksonWeber Yes, I was able to collect mostly Dependencies. But also, I've seen the collected traces & requests, even 1 exception. But somehow, no winson logs were visible in the traces.

@JacksonWeber
Copy link
Contributor

@not-aesthetic-code I'm not exactly sure in what order your app will load these components. But based on the fact that you're seeing other telemetry with the exception of Winston, it seems that the issue lies specifically with instrumenting Winston. It's important that the Application Insights SDK is initialized before Winston so that it can be properly instrumented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants