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
.
$ npm i winston-format-ec2
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'
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
})
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,
})