Skip to content

Commit

Permalink
feat(server): add logging options (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
sugarshin committed Jan 16, 2023
1 parent 6e3070f commit 6c5840d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/server/logging-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import pinoHttp from "pino-http";
import type { Logger } from "pino";
import { v4 as uuidv4 } from "uuid";

export function getLoggingMiddleware(logger: Logger) {
export function getLoggingMiddleware(logger: Logger, options?: pinoHttp.Options) {
return pinoHttp({
...options,
logger: logger.child({ name: "http" }),
customSuccessMessage(res) {
const responseTime = Date.now() - res[pinoHttp.startTime];
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Server {
webhookProxy: options.webhookProxy,
};

this.expressApp.use(getLoggingMiddleware(this.log));
this.expressApp.use(getLoggingMiddleware(this.log, options.loggingOptions));
this.expressApp.use(
"/probot/static/",
express.static(join(__dirname, "..", "..", "static"))
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@octokit/webhooks";
import LRUCache from "lru-cache";
import Redis from "ioredis";
import { Options as LoggingOptions } from "pino-http";

import { Probot } from "./index";
import { Context } from "./context";
Expand Down Expand Up @@ -64,6 +65,7 @@ export type ServerOptions = {
webhookPath?: string;
webhookProxy?: string;
Probot: typeof Probot;
loggingOptions?: LoggingOptions;
};

export type MiddlewareOptions = {
Expand Down
33 changes: 28 additions & 5 deletions test/server/logging-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import Stream from "stream";
import express from "express";
import request from "supertest";
import pino from "pino";
import { Options } from "pino-http";

import { getLoggingMiddleware } from "../../src/server/logging-middleware";

describe("logging", () => {
let server: express.Express;
let output: any[];
let options: Options;

const streamLogsToOutput = new Stream.Writable({ objectMode: true });
streamLogsToOutput._write = (object, encoding, done) => {
Expand All @@ -17,20 +19,24 @@ describe("logging", () => {
};
const logger = pino(streamLogsToOutput);

beforeEach(() => {
server = express();
output = [];

function applyMiddlewares() {
server.use(express.json());
server.use(getLoggingMiddleware(logger));
server.use(getLoggingMiddleware(logger, options));
server.get("/", (req, res) => {
res.set("X-Test-Header", "testing");
res.send("OK");
});
server.post("/", (req, res) => res.send("OK"));
}

beforeEach(() => {
server = express();
output = [];
options = {};
});

test("logs requests and responses", () => {
applyMiddlewares();
return request(server)
.get("/")
.expect(200)
Expand Down Expand Up @@ -62,6 +68,7 @@ describe("logging", () => {
});

test("uses supplied X-Request-ID", () => {
applyMiddlewares();
return request(server)
.get("/")
.set("X-Request-ID", "42")
Expand All @@ -72,6 +79,7 @@ describe("logging", () => {
});

test("uses X-GitHub-Delivery", () => {
applyMiddlewares();
return request(server)
.get("/")
.set("X-GitHub-Delivery", "a-b-c")
Expand All @@ -80,4 +88,19 @@ describe("logging", () => {
expect(output[0].req.id).toEqual("a-b-c");
});
});

test("sets ignorePaths option to ignore logging", () => {
options = {
autoLogging: {
ignorePaths: ["/"],
},
};
applyMiddlewares();
return request(server)
.get("/")
.expect(200)
.expect((res) => {
expect(output.length).toEqual(0);
});
});
});

0 comments on commit 6c5840d

Please sign in to comment.