Repository: iamolegga/nestjs-pino (ts)

Issue:
[FEATURE REQUEST] make the underlying pino instance accessible
**Is your feature request related to a problem? Please describe.**
The underlying [pino instance is private](https://github.com/iamolegga/nestjs-pino/blob/99247e6c64732689c5de8b8dabcaa311bf95c632/src/PinoLogger.ts#L110-L112).

**Describe the solution you'd like**
Make it public or at least protected such that one could access it with the certainty that a minor/patch version update of this package does not remove/rename the getter.

**Describe alternatives you've considered**
Creating a separate pino instance to pass down to the parts of the system that need a `Logger` would be an alternative. This would make it quite a lot more complicated though.

**Additional context**
Some of the business logic in our projects is generic in a way that it does not know about NestJS specifics, but knows about pino (especially its `child()` method). These are given a pino logger from the NestJS specific parts (e.g. controllers). If we do not use the private getter that is linked above, we would need to maintain a separate second pino instance, hence, not gaining anything from using such a sweet package as `nestjs-pino`.

Pull request:
make PinoLogger.logger public
Fix #439 

Patch:
diff --git a/src/PinoLogger.ts b/src/PinoLogger.ts
index cea60f4..d536a8c 100644
--- a/src/PinoLogger.ts
+++ b/src/PinoLogger.ts
@@ -107,7 +107,7 @@ export class PinoLogger implements PinoMethods {
     (this.logger[method] as any)(...args);
   }
 
-  private get logger() {
+  public get logger() {
     return getValue<pino.Logger>(LOGGER_KEY) || outOfContext;
   }
 }


Test patch:
diff --git a/__tests__/logger-instance.spec.ts b/__tests__/logger-instance.spec.ts
new file mode 100644
index 0000000..7ef7616
--- /dev/null
+++ b/__tests__/logger-instance.spec.ts
@@ -0,0 +1,60 @@
+import { NestFactory } from "@nestjs/core";
+import { Module, Controller, Get } from "@nestjs/common";
+import MemoryStream = require("memorystream");
+import * as request from "supertest";
+import { PinoLogger, InjectPinoLogger, LoggerModule } from "../src";
+import { platforms } from "./utils/platforms";
+import { fastifyExtraWait } from "./utils/fastifyExtraWait";
+import { __resetOutOfContextForTests } from "../src/PinoLogger";
+
+describe("getting the logger instance", () => {
+  beforeEach(() => __resetOutOfContextForTests());
+
+  for (const PlatformAdapter of platforms) {
+    describe(PlatformAdapter.name, () => {
+      it("logger is publicly accessible", async () => {
+        const stream = new MemoryStream();
+        const context = Math.random().toString();
+        let logs = "";
+
+        stream.on("data", (chunk: string) => {
+          logs += chunk.toString();
+        });
+
+        @Controller("/")
+        class TestController {
+          constructor(
+            @InjectPinoLogger(context) private readonly logger: PinoLogger
+          ) {}
+          @Get()
+          get() {
+            return {
+              constructorName: this.logger.logger.constructor.name,
+            };
+          }
+        }
+
+        @Module({
+          imports: [LoggerModule.forRoot({ pinoHttp: stream })],
+          controllers: [TestController],
+        })
+        class TestModule {}
+
+        const app = await NestFactory.create(
+          TestModule,
+          new PlatformAdapter(),
+          { logger: false }
+        );
+        const server = app.getHttpServer();
+
+        await app.init();
+        await fastifyExtraWait(PlatformAdapter, app);
+
+        const response = await request(server).get("/");
+        expect(response.body.constructorName).toEqual("Pino");
+
+        await app.close();
+      });
+    });
+  }
+});