I want to send all my Nestjs logs to Grafana, using a winston logger service. I tried multiple things, but none of them is working...
// main.ts
const logger = new LoggerService();
const app = await NestFactory.create(AppModule, {
logger: logger,
});
app.useLogger(logger);
// logger.service.ts
import { LoggerService as LS } from '@nestjs/common';
import winston from 'winston';
const { combine, timestamp } = winston.format;
import * as Transport from 'winston-transport';
import {
utilities as nestWinstonModuleUtilities,
WinstonModule,
} from 'nest-winston';
import LokiTransport from 'winston-loki';
export class LoggerService implements LS {
private logger: LS;
constructor() {
this.logger = WinstonModule.createLogger({
transports: this.logTransports(),
});
}
private logTransports = () => {
const logLevel = !!process.env.LOG_LEVEL ? process.env.LOG_LEVEL : 'error';
const format = combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
nestWinstonModuleUtilities.format.nestLike(),
);
const logTransports: Transport[] = [
new LokiTransport({
host: process.env.G_URL,
basicAuth: `${process.env.G_USER}:${process.env.G_KEY}`,
labels: { app: process.env.G_NAME },
json: true,
level: logLevel,
format: winston.format.json(),
replaceTimestamp: true,
timeout: 10,
onConnectionError: (err) => console.error(err),
}),
new winston.transports.Console({
format: format,
level: logLevel,
}),
];
return logTransports;
};
// + other functions (toPrettyJson, log, error, etc)
}
and my env variables are correct (ps: censored them using ...)
G_URL="https://logs-prod-....grafana.net"
G_NAME="grafanacloud-...-logs"
G_USER="8...5"
G_KEY="glc_eyJ...ifX0="
LOG_LEVEL='debug'
I also tried different ways of creating the transporst or logger, but none sending any logs to Grafana... I see logs in my console, formatted as expected though. No errors.
I also followed and tried all these things https://github.com/JaniAnttonen/winston-loki/issues/140, but with no success...
I also tried a local Docker Loki image, with no success of sending any logs into it. I also got timeout errors for that :)
Any ideas? Am I doing something wrong? Do I need to create a server, and use Grafana just for their UI & search capabilities? :thinking: