googleapis / nodejs-logging-winston

Node.js client integration between Stackdriver Logging and Winston.
https://cloud.google.com/logging/
Apache License 2.0
105 stars 50 forks source link

Using LoggingWinston express middelware cases crash #813

Closed macrozone closed 9 months ago

macrozone commented 11 months ago

this is similar to this issue https://github.com/googleapis/nodejs-logging-winston/issues/623

its completly natural that you use this library first locally, where there is no connection to any google cloud project and you don't want to push the logs to google cloud

even if you pass a local transport to the express middleware, it crashes with

Error: Unable to detect a Project Id in the current environment.
To learn more about authentication and Google APIs, visit:
https://cloud.google.com/docs/authentication/getting-started

the logger and middleware is created here:

import winston, { format } from "winston"
import { LoggingWinston, express } from "@google-cloud/logging-winston"

const googleCloudLogger = new LoggingWinston({
  redirectToStdout: true,
  logName: "app",
})

const defaultTransport =
  process.env.GCLOUD_LOGS === "1"
    ? googleCloudLogger
    : new winston.transports.Console({
      // ....
      })
export const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || "info",
  transports: [defaultTransport],
})

export const makeExpressMiddleware = async () => express.makeMiddleware(logger)

calling the app with passing GOOGLE_PROJECT variable also does not work

Environment details

Steps to reproduce

  1. just use the express middleware provided by this package
  2. try to use it locally
cindy-peng commented 9 months ago

Hi @macrozone, as mentioned in https://github.com/googleapis/nodejs-logging-winston/issues/623, google cloud logging libraries are intended to write logs into Google Cloud Logging service and we do not support "local" mode of work.

If you still want to have this working without crashing, you can do the following:

  1. Set up "project_id" environment variable before running your application: export GCLOUD_PROJECT=your_project_id. I tried your code sample with this variable specified in my environment, it worked for me without crashing. You can also define the desired project id via LoggingOptions.projectId parameter.

  2. As mentioned here, express middleware is still an experimental feature. This library is not intended for supporting local mode, we do require "project_id" to composite the logging path where log entries are written to. If you really want to use the logger without specifying the project_id locally, instead of using express middleware, you can use this logger without middleware and add a default call back for error handling: https://github.com/googleapis/nodejs-logging-winston#error-handling-with-a-default-callback

cindy-peng commented 9 months ago

Closing the issue as it is intended that logging libraries don't support "local" mode. Please set up "project_id" through environment variable orLoggingOptions.projectId. You can also use default callback to add error handling for your environment.