Last active
October 1, 2024 18:52
-
-
Save mrister/0640724a2cb56fd97114c9c273226178 to your computer and use it in GitHub Desktop.
Logging to Loggly in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run npm i -D @types/winston | |
// run npm i winston | |
import {Logger, transports} from 'winston'; | |
// run npm i winston-loggly-bulk | |
require('winston-loggly-bulk'); | |
const logger = new Logger({ | |
transports: [ | |
new transports.Console(), | |
new transports.Loggly({ | |
token: 'YOUR_TOKEN', | |
subdomain: 'YOUR_SUBDOMAIN', | |
json: true, | |
}) | |
] | |
}); | |
// define interface to only log a part of data | |
interface Loggable { | |
id?: string, | |
getLog(): string | |
description?: string | |
} | |
const message: Loggable = { | |
id: 'test', | |
getLog() { | |
return 'Hello from TypeScript!!'; | |
} | |
// there is no description since it is optional | |
}; | |
// function to use only our Loggable interface | |
function logMessage (message: Loggable) { | |
logger.info(message.getLog()); | |
} | |
// log something | |
logMessage(message); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting an error on this line:
https://gist.github.com/mrister/0640724a2cb56fd97114c9c273226178#file-loggly-winston-typescript-ts-L10
saying that Loggly is not recognized in transports