Search code examples
node.jsloggingdigital-oceanwinston

Logging a nodejs application into DigitalOcean Spaces using Winston


Does Winston support a writable stream object that uploads to DigitalOcean Spaces?

There is for example s3-streamlogger for S3 objects, but I could not find a direct way to use winston with spaces.


Solution

  • According to Spaces Documentation, Spaces is compatible with AWS S3 API:

    The Spaces API aims to be interoperable with Amazon's AWS S3 API. In most cases, when using a client library, setting the "endpoint" or "base" URL to ${REGION}.digitaloceanspaces.com and generating a Spaces key to replace your AWS IAM key will allow you to use Spaces in place of S3.

    So I ended up using s3-streamlogger with Winston to upload logs into my spaces bucket:

    import   winston          from 'winston';
    import { S3StreamLogger } from 's3-streamlogger';
    
    const s3Stream = new S3StreamLogger({
      bucket: "mybucket",
      config: {
        endpoint: 'nyc3.digitaloceanspaces.com',
      },
      access_key_id: "MY_ACCESS_KEY",
      secret_access_key: "MY_SECRET_KEY",
      tags: {type: "mytype", project: "myproject"}
    });
    
    const s3Transport = new winston.transports.Stream({
      stream: s3Stream
    });
    
    export const logger = winston.createLogger({
      transports: [s3Transport]
    });
    
    logger.info('Hello Winston!');
    

    Hope it helps