NestJS Logger for Google Cloud Run
Installation
- Install the dependency
yarn add @nest-gcr/logger
- Register the module
import { Module } from '@nestjs/common';
import { LoggerModule } from '@nest-gcr/logger';
@Module({
imports: [LoggerModule]
})
export class AppModule {}
Usage
- Use it anywhere in your application. You have access to two providers:
import { Inject, Injectable } from '@nestjs/common';
import { LOGGER, Logger } from '@nest-gcr/logger'
@Injectable()
export class MyProvider {
constructor(
@Inject(LOGGER.PROVIDERS.LOGGER) logger: Logger,
@Inject(LOGGER.PROVIDERS.REQUEST_LOGGER) requestLogger: Logger
) {}
foo () {
this.requestLogger.debug('Hello World!')
}
}
- Use the global logger for default nestJS logs:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { rootLogger } from '@nest-gcr/logger';
async function bootstrap() {
const app = await NestFactory.create(TestModule, {
logger: {
...rootLogger,
log: (message, parameters) => {
return rootLogger.info(message, parameters);
},
error: (message, parameters) => {
return rootLogger.error(message, parameters)
}
},
});
await app.listen(process.env.PORT || 3000);
}
bootstrap();
Configuration on Google Cloud RUN
In order for logs to be correctly logged to Google Cloud Logging from Cloud Run instances, you need to set the following environment variable into your container:
LOGGER_DRIVER=gcp
Change Log
See Changelog for more information.
Contributing
Contributions welcome! See Contributing.
Author
John Biundo (Y Prospect on Discord)
License
Licensed under the MIT License - see the LICENSE file for details.