mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 19:28:16 +00:00
Replace winston with pino
This commit is contained in:
Generated
+772
-1037
File diff suppressed because it is too large
Load Diff
@@ -42,6 +42,9 @@
|
|||||||
"passport-github": "^1.1.0",
|
"passport-github": "^1.1.0",
|
||||||
"passport-gitlab2": "^5.0.0",
|
"passport-gitlab2": "^5.0.0",
|
||||||
"passport-google-oauth20": "^2.0.0",
|
"passport-google-oauth20": "^2.0.0",
|
||||||
|
"pino": "^8.16.1",
|
||||||
|
"pino-http": "^8.5.1",
|
||||||
|
"pino-pretty": "^10.2.3",
|
||||||
"posthog-node": "^2.6.0",
|
"posthog-node": "^2.6.0",
|
||||||
"probot": "^12.3.1",
|
"probot": "^12.3.1",
|
||||||
"query-string": "^7.1.3",
|
"query-string": "^7.1.3",
|
||||||
@@ -52,8 +55,6 @@
|
|||||||
"tweetnacl-util": "^0.15.1",
|
"tweetnacl-util": "^0.15.1",
|
||||||
"typescript": "^4.9.3",
|
"typescript": "^4.9.3",
|
||||||
"utility-types": "^3.10.0",
|
"utility-types": "^3.10.0",
|
||||||
"winston": "^3.8.2",
|
|
||||||
"winston-loki": "^6.0.6",
|
|
||||||
"zod": "^3.22.3"
|
"zod": "^3.22.3"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
@@ -104,6 +105,7 @@
|
|||||||
"@types/nodemailer": "^6.4.6",
|
"@types/nodemailer": "^6.4.6",
|
||||||
"@types/passport": "^1.0.12",
|
"@types/passport": "^1.0.12",
|
||||||
"@types/picomatch": "^2.3.0",
|
"@types/picomatch": "^2.3.0",
|
||||||
|
"@types/pino": "^7.0.5",
|
||||||
"@types/supertest": "^2.0.12",
|
"@types/supertest": "^2.0.12",
|
||||||
"@types/swagger-jsdoc": "^6.0.1",
|
"@types/swagger-jsdoc": "^6.0.1",
|
||||||
"@types/swagger-ui-express": "^4.1.3",
|
"@types/swagger-ui-express": "^4.1.3",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { getLogger } from "../utils/logger";
|
import { logger } from "../utils/logging";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize database connection
|
* Initialize database connection
|
||||||
@@ -18,10 +18,10 @@ export const initDatabaseHelper = async ({
|
|||||||
// allow empty strings to pass the required validator
|
// allow empty strings to pass the required validator
|
||||||
mongoose.Schema.Types.String.checkRequired(v => typeof v === "string");
|
mongoose.Schema.Types.String.checkRequired(v => typeof v === "string");
|
||||||
|
|
||||||
(await getLogger("database")).info("Database connection established");
|
logger.info("Database connection established");
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
(await getLogger("database")).error(`Unable to establish Database connection due to the error.\n${err}`);
|
logger.error(err, "Unable to establish database connection");
|
||||||
}
|
}
|
||||||
|
|
||||||
return mongoose.connection;
|
return mongoose.connection;
|
||||||
|
|||||||
+14
-4
@@ -5,6 +5,8 @@ import express from "express";
|
|||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
|
import { logger } from "./utils/logging";
|
||||||
|
import httpLogger from "pino-http";
|
||||||
import { DatabaseService } from "./services";
|
import { DatabaseService } from "./services";
|
||||||
import { EELicenseService, GithubSecretScanningService } from "./ee/services";
|
import { EELicenseService, GithubSecretScanningService } from "./ee/services";
|
||||||
import { setUpHealthEndpoint } from "./services/health";
|
import { setUpHealthEndpoint } from "./services/health";
|
||||||
@@ -73,7 +75,7 @@ import {
|
|||||||
workspaces as v3WorkspacesRouter
|
workspaces as v3WorkspacesRouter
|
||||||
} from "./routes/v3";
|
} from "./routes/v3";
|
||||||
import { healthCheck } from "./routes/status";
|
import { healthCheck } from "./routes/status";
|
||||||
import { getLogger } from "./utils/logger";
|
// import { getLogger } from "./utils/logger";
|
||||||
import { RouteNotFoundError } from "./utils/errors";
|
import { RouteNotFoundError } from "./utils/errors";
|
||||||
import { requestErrorHandler } from "./middleware/requestErrorHandler";
|
import { requestErrorHandler } from "./middleware/requestErrorHandler";
|
||||||
import {
|
import {
|
||||||
@@ -94,12 +96,20 @@ import path from "path";
|
|||||||
let handler: null | any = null;
|
let handler: null | any = null;
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
|
const port = await getPort();
|
||||||
|
|
||||||
await setup();
|
await setup();
|
||||||
|
|
||||||
await EELicenseService.initGlobalFeatureSet();
|
await EELicenseService.initGlobalFeatureSet();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.enable("trust proxy");
|
app.enable("trust proxy");
|
||||||
|
|
||||||
|
app.use(httpLogger({
|
||||||
|
logger,
|
||||||
|
autoLogging: false
|
||||||
|
}));
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: false }));
|
app.use(express.urlencoded({ extended: false }));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
@@ -164,7 +174,7 @@ const main = async () => {
|
|||||||
const nextApp = new NextServer({
|
const nextApp = new NextServer({
|
||||||
dev: false,
|
dev: false,
|
||||||
dir: nextJsBuildPath,
|
dir: nextJsBuildPath,
|
||||||
port: await getPort(),
|
port,
|
||||||
conf,
|
conf,
|
||||||
hostname: "local",
|
hostname: "local",
|
||||||
customServer: false
|
customServer: false
|
||||||
@@ -255,8 +265,8 @@ const main = async () => {
|
|||||||
|
|
||||||
app.use(requestErrorHandler);
|
app.use(requestErrorHandler);
|
||||||
|
|
||||||
const server = app.listen(await getPort(), async () => {
|
const server = app.listen(port, async () => {
|
||||||
(await getLogger("backend-main")).info(`Server started listening at port ${await getPort()}`);
|
logger.info(`Server started listening at port ${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// await createTestUserForDevelopment();
|
// await createTestUserForDevelopment();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import * as Sentry from "@sentry/node";
|
|||||||
import { ErrorRequestHandler } from "express";
|
import { ErrorRequestHandler } from "express";
|
||||||
import { TokenExpiredError } from "jsonwebtoken";
|
import { TokenExpiredError } from "jsonwebtoken";
|
||||||
import { InternalServerError, UnauthorizedRequestError } from "../utils/errors";
|
import { InternalServerError, UnauthorizedRequestError } from "../utils/errors";
|
||||||
import { getLogger } from "../utils/logger";
|
import { logger } from "../utils/logging";
|
||||||
import RequestError from "../utils/requestError";
|
import RequestError from "../utils/requestError";
|
||||||
import { ForbiddenError } from "@casl/ability";
|
import { ForbiddenError } from "@casl/ability";
|
||||||
|
|
||||||
@@ -15,10 +15,14 @@ export const requestErrorHandler: ErrorRequestHandler = async (
|
|||||||
if (res.headersSent) return next();
|
if (res.headersSent) return next();
|
||||||
|
|
||||||
const logAndCaptureException = async (error: RequestError) => {
|
const logAndCaptureException = async (error: RequestError) => {
|
||||||
(await getLogger("backend-main")).log(
|
// TODO: tie pino error-handling to error types/levels
|
||||||
(<RequestError>error).levelName.toLowerCase(),
|
|
||||||
`${error.stack}\n${error.message}`
|
// (await getLogger("backend-main")).log(
|
||||||
);
|
// (<RequestError>error).levelName.toLowerCase(),
|
||||||
|
// `${error.stack}\n${error.message}`
|
||||||
|
// );
|
||||||
|
|
||||||
|
logger.error(error.stack, error.message);
|
||||||
|
|
||||||
//* Set Sentry user identification if req.user is populated
|
//* Set Sentry user identification if req.user is populated
|
||||||
if (req.user !== undefined && req.user !== null) {
|
if (req.user !== undefined && req.user !== null) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { PostHog } from "posthog-node";
|
import { PostHog } from "posthog-node";
|
||||||
import { getLogger } from "../utils/logger";
|
import { logger } from "../utils/logging";
|
||||||
import { AuthData } from "../interfaces/middleware";
|
import { AuthData } from "../interfaces/middleware";
|
||||||
import {
|
import {
|
||||||
getNodeEnv,
|
getNodeEnv,
|
||||||
@@ -22,13 +22,13 @@ class Telemetry {
|
|||||||
* Logs telemetry enable/disable notice.
|
* Logs telemetry enable/disable notice.
|
||||||
*/
|
*/
|
||||||
static logTelemetryMessage = async () => {
|
static logTelemetryMessage = async () => {
|
||||||
|
|
||||||
if(!(await getTelemetryEnabled())){
|
if(!(await getTelemetryEnabled())){
|
||||||
(await getLogger("backend-main")).info([
|
[
|
||||||
"",
|
|
||||||
"To improve, Infisical collects telemetry data about general usage.",
|
"To improve, Infisical collects telemetry data about general usage.",
|
||||||
"This helps us understand how the product is doing and guide our product development to create the best possible platform; it also helps us demonstrate growth as we support Infisical as open-source software.",
|
"This helps us understand how the product is doing and guide our product development to create the best possible platform; it also helps us demonstrate growth as we support Infisical as open-source software.",
|
||||||
"To opt into telemetry, you can set `TELEMETRY_ENABLED=true` within the environment variables.",
|
"To opt into telemetry, you can set `TELEMETRY_ENABLED=true` within the environment variables.",
|
||||||
].join("\n"))
|
].forEach(line => logger.info(line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { createTerminus } from "@godaddy/terminus";
|
import { createTerminus } from "@godaddy/terminus";
|
||||||
import { getLogger } from "../utils/logger";
|
import { logger } from "../utils/logging";
|
||||||
|
|
||||||
export const setUpHealthEndpoint = <T>(server: T) => {
|
export const setUpHealthEndpoint = <T>(server: T) => {
|
||||||
const onSignal = async () => {
|
const onSignal = async () => {
|
||||||
(await getLogger("backend-main")).info("Server is starting clean-up");
|
logger.info("Server is starting clean-up");
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
if (mongoose.connection && mongoose.connection.readyState == 1) {
|
if (mongoose.connection && mongoose.connection.readyState == 1) {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
getSmtpSecure,
|
getSmtpSecure,
|
||||||
getSmtpUsername,
|
getSmtpUsername,
|
||||||
} from "../config";
|
} from "../config";
|
||||||
import { getLogger } from "../utils/logger";
|
import { logger } from "../utils/logging";
|
||||||
|
|
||||||
export const initSmtp = async () => {
|
export const initSmtp = async () => {
|
||||||
const mailOpts: SMTPConnection.Options = {
|
const mailOpts: SMTPConnection.Options = {
|
||||||
@@ -84,15 +84,14 @@ export const initSmtp = async () => {
|
|||||||
.then(async () => {
|
.then(async () => {
|
||||||
Sentry.setUser(null);
|
Sentry.setUser(null);
|
||||||
Sentry.captureMessage("SMTP - Successfully connected");
|
Sentry.captureMessage("SMTP - Successfully connected");
|
||||||
(await getLogger("backend-main")).info(
|
logger.info("SMTP - Successfully connected");
|
||||||
"SMTP - Successfully connected"
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
.catch(async (err) => {
|
.catch(async (err) => {
|
||||||
Sentry.setUser(null);
|
Sentry.setUser(null);
|
||||||
Sentry.captureException(
|
Sentry.captureException(
|
||||||
`SMTP - Failed to connect to ${await getSmtpHost()}:${await getSmtpPort()} \n\t${err}`
|
`SMTP - Failed to connect to ${await getSmtpHost()}:${await getSmtpPort()} \n\t${err}`
|
||||||
);
|
);
|
||||||
|
logger.error(err, `SMTP - Failed to connect to ${await getSmtpHost()}:${await getSmtpPort()}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
return transporter;
|
return transporter;
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
/* eslint-disable no-console */
|
|
||||||
import { createLogger, format, transports } from "winston";
|
|
||||||
import LokiTransport from "winston-loki";
|
|
||||||
import { getLokiHost, getNodeEnv } from "../config";
|
|
||||||
|
|
||||||
const { combine, colorize, label, printf, splat, timestamp } = format;
|
|
||||||
|
|
||||||
const logFormat = (prefix: string) => combine(
|
|
||||||
timestamp(),
|
|
||||||
splat(),
|
|
||||||
label({ label: prefix }),
|
|
||||||
printf((info) => `${info.timestamp} ${info.label} ${info.level}: ${info.message}`)
|
|
||||||
);
|
|
||||||
|
|
||||||
const createLoggerWithLabel = async (level: string, label: string) => {
|
|
||||||
const _level = level.toLowerCase() || "info"
|
|
||||||
//* Always add Console output to transports
|
|
||||||
const _transports: any[] = [
|
|
||||||
new transports.Console({
|
|
||||||
format: combine(
|
|
||||||
colorize(),
|
|
||||||
logFormat(label),
|
|
||||||
// format.json()
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
//* Add LokiTransport if it's enabled
|
|
||||||
if((await getLokiHost()) !== undefined){
|
|
||||||
_transports.push(
|
|
||||||
new LokiTransport({
|
|
||||||
host: await getLokiHost(),
|
|
||||||
handleExceptions: true,
|
|
||||||
handleRejections: true,
|
|
||||||
batching: true,
|
|
||||||
level: _level,
|
|
||||||
timeout: 30000,
|
|
||||||
format: format.combine(
|
|
||||||
format.json()
|
|
||||||
),
|
|
||||||
labels: {
|
|
||||||
app: process.env.npm_package_name,
|
|
||||||
version: process.env.npm_package_version,
|
|
||||||
environment: await getNodeEnv(),
|
|
||||||
},
|
|
||||||
onConnectionError: (err: Error)=> console.error("Connection error while connecting to Loki Server.\n", err),
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return createLogger({
|
|
||||||
level: _level,
|
|
||||||
transports: _transports,
|
|
||||||
format: format.combine(
|
|
||||||
logFormat(label),
|
|
||||||
format.metadata({ fillExcept: ["message", "level", "timestamp", "label"] })
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getLogger = async (loggerName: "backend-main" | "database") => {
|
|
||||||
const logger = {
|
|
||||||
"backend-main": await createLoggerWithLabel("info", "[IFSC:backend-main]"),
|
|
||||||
"database": await createLoggerWithLabel("info", "[IFSC:database]"),
|
|
||||||
}
|
|
||||||
return logger[loggerName]
|
|
||||||
}
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export { logger } from "./logger";
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import pino from "pino";
|
||||||
|
|
||||||
|
export const logger = pino({
|
||||||
|
level: process.env.PINO_LOG_LEVEL || "trace",
|
||||||
|
timestamp: pino.stdTimeFunctions.isoTime,
|
||||||
|
formatters: {
|
||||||
|
bindings: (bindings) => {
|
||||||
|
return {
|
||||||
|
pid: bindings.pid,
|
||||||
|
hostname: bindings.hostname
|
||||||
|
// node_version: process.version
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
transport: {
|
||||||
|
target: "pino-pretty",
|
||||||
|
options: {
|
||||||
|
colorize: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user