Skip to main content

Winston logging to OpenObserve

Centralized logging for Node.js apps.

  1. Install OpenObserve. Installation is simply downloading and unzipping the single binary. OpenObserve runs externally by default with no option to change this behavior, so you may need a firewall to restrict the access. I used ufw.
  2. Create a 'data' directory in the same location as the OpenObserve binary. Start OpenObserve:
    ZO_ROOT_USER_EMAIL="<email>" ZO_ROOT_USER_PASSWORD="<password>" ZO_DATA_DIR="$PWD/data" ./openobserve > /dev/null 2>&1 &
  3. Add winston as a dependency to your Node.js app.
  4. Create logger.js:
    import winston, {transports} from "winston";
    
    const options = {
        level: 'debug',
        host: 'localhost',
        port: '5080',
        path: '/api/<organization>/<stream>/_json',
        auth: {
            username: '<email>',
            password: '<password>',
        },
        ssl: false,
        batch: true,
        batchInterval: 5000,
        batchCount: 10,
    };
    
    export const log = winston.createLogger({
        transports: new transports.Http(options),
        exceptionHandlers: new transports.Http(options),
        exitOnError: false
    })
    
  5. Import and use the logger in the application:
    import {log} from "$lib/logger.js";
    
    export async function handle({ event, resolve }) {
        log.debug(event.request.method + ' ' + event.url.pathname + event.url.search);
        ...
  6. Run the application and view the logs in OpenObserve:
    Screenshot from 2023-07-06 11-48-02.png