refactor: replace @axiomhq/pino with @axiomhq/js
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { Axiom } from "@axiomhq/js";
|
||||
import pino from "pino";
|
||||
|
||||
const isDev = process.env.NODE_ENV !== "production";
|
||||
@@ -8,34 +9,29 @@ const axiomToken = process.env.AXIOM_TOKEN;
|
||||
const axiomDataset = process.env.AXIOM_DATASET;
|
||||
const useAxiom = isCloud && !!axiomToken && !!axiomDataset;
|
||||
|
||||
const targets: pino.TransportTargetOptions[] = [];
|
||||
|
||||
if (isDev) {
|
||||
targets.push({
|
||||
target: "pino-pretty",
|
||||
options: {
|
||||
colorize: true,
|
||||
ignore: "pid,hostname",
|
||||
translateTime: "HH:MM:ss",
|
||||
function createAxiomStream(token: string, dataset: string): pino.DestinationStream {
|
||||
const client = new Axiom({ token });
|
||||
return {
|
||||
write(msg: string) {
|
||||
try {
|
||||
client.ingest(dataset, [JSON.parse(msg) as Record<string, unknown>]);
|
||||
} catch {
|
||||
// ignore malformed log lines
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
if (useAxiom) {
|
||||
targets.push({
|
||||
target: "@axiomhq/pino",
|
||||
options: {
|
||||
dataset: axiomDataset,
|
||||
token: axiomToken,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const logger = pino({
|
||||
level,
|
||||
...(targets.length > 0 && {
|
||||
transport: { targets },
|
||||
}),
|
||||
});
|
||||
export const logger = useAxiom
|
||||
? pino({ level }, pino.multistream([{ stream: createAxiomStream(axiomToken, axiomDataset), level }]))
|
||||
: pino({
|
||||
level,
|
||||
...(isDev && {
|
||||
transport: {
|
||||
target: "pino-pretty",
|
||||
options: { colorize: true, ignore: "pid,hostname", translateTime: "HH:MM:ss" },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
export const createLogger = (module: string) => logger.child({ module });
|
||||
|
||||
Reference in New Issue
Block a user