winston-format-ec2
v1.0.0-beta.4
Published
A winston format for prepending AWS EC2 instance id onto each log statement.
Downloads
7
Readme
winston-format-ec2
A Winston (>= 3.x) format for prepending the AWS EC2 instance ID (e.g. i-abc123def456
) onto your log statements.
The instance ID is loaded dynamically via the aws-sdk utilizing the MetadataService
.
Installation
Installing
$ npm i winston-format-ec2
Usage
import winston from "winston"
import { ec2WinstonFormat } from "winston-format-ec2"
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.simple(),
ec2WinstonFormat(),
),
level: "silly",
transports: [
new winston.transports.Console(),
],
})
logger.silly("You silly log statement") // Outputs 'i-abc123def456 silly: You silly log statement'
Format Options
ec2WinstonFormat({
color: "\u001b", // If you want the instance ID to be colored
maxLength: 7, // To take only the first few characters of the instance ID instead of the whole thing
})
Injecting MetadataService
You can also inject an instance of the AWS MetadataService:
import AWS from "aws-sdk"
import { ec2WinstonFormat } from "winston-format-ec2"
const metadataService = new AWS.MetadataService()
const format = ec2WinstonFormat({
awsMetadataService: metadataService,
})