cloud-app-logger
TypeScript icon, indicating that this package has built-in type declarations

0.0.18 • Public • Published

Adding Cloud Logging to Express Application

This guide explains how to integrate cloud logging into your Express application using the cloud-app-logger library. Each log will include a unique request ID for tracking the entire journey of a request, including request body, response, and any additional logs or errors for that request.

Note: Currently, only Google Cloud Logging is supported.

Steps to Add Cloud Logging

  1. Add the following to your package.json:

    "cloud-app-logger": "^0.0.17"
  2. Add the following environment variables:

    LOGGING_PROJECT_ID=<YOUR_PROJECT_ID> # Name for the Google Cloud project where logs should be stored
    LOGGING_ENABLED=true # Enable logs being sent to the cloud. Keep false when running local code
    LOGGING_LOG_NAME=<LOG_NAME> # Log name in Google Cloud. For example, app-name-staging
    
  3. Import the necessary functions wherever your Express app object is created:

    import { initAppLogger, configureErrorLogging, configureRequestLogging } from 'cloud-app-logger';
    
    // Call this as early as possible but after calling dotenv.config()
    initAppLogger();
  4. After adding body parsing middlewares such as json or urlencoded, configure request logging:

    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    
    // Call the function here !!!
    configureRequestLogging(app);
  5. Call the error logging configuration function after adding all middlewares/routes:

    configureErrorLogging(app);
  6. Call the logUnhandledExceptions function in each file where you have defined a router after importing Router or Express:

    import { Router, Request, Response } from 'express';
    import { logUnhandledExceptions } from 'cloud-app-logger';
    
    // Call the function here
    logUnhandledExceptions();
    
    const router = Router();
  7. Run the project, and logging should now be enabled and visible in Google Cloud Logging.

Readme

Keywords

none

Package Sidebar

Install

npm i cloud-app-logger

Weekly Downloads

60

Version

0.0.18

License

ISC

Unpacked Size

9.06 kB

Total Files

6

Last publish

Collaborators

  • ranapushpender