Skip to content

Commit

Permalink
wip [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko committed Jun 27, 2024
1 parent b35ce72 commit 8de9705
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 109 deletions.
1 change: 0 additions & 1 deletion .kuzzlerc.sample.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
"maxConcurrentPipes": 50,
"pipesBufferSize": 50000,
"include": [
"kuzzle-plugin-logger",
"kuzzle-plugin-auth-passport-local"
]
},
Expand Down
2 changes: 1 addition & 1 deletion lib/config/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const defaultConfig: KuzzleConfiguration = {
initTimeout: 10000,
maxConcurrentPipes: 50,
pipesBufferSize: 50000,
include: ["kuzzle-plugin-logger", "kuzzle-plugin-auth-passport-local"],
include: ["kuzzle-plugin-auth-passport-local"],
},
"kuzzle-plugin-logger": {
services: {
Expand Down
5 changes: 1 addition & 4 deletions lib/core/plugin/pluginsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ const strategyError = kerror.wrap("plugin", "strategy");
const controllerError = kerror.wrap("plugin", "controller");

// Without those plugins, Kuzzle won't start at all.
const CORE_PLUGINS = [
"kuzzle-plugin-logger",
"kuzzle-plugin-auth-passport-local",
];
const CORE_PLUGINS = ["kuzzle-plugin-auth-passport-local"];

/**
* @class PluginsManager
Expand Down
178 changes: 178 additions & 0 deletions lib/kuzzle/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2022 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ecsFormat } from "@elastic/ecs-pino-format";
import { pino, Logger as PinoLogger } from "pino";

/**
* The Logger class provides logging functionality for Kuzzle.
*/
export class Logger {
private pino: PinoLogger<"silly" | "verbose">;
private failsafeModeString: string;

constructor() {
const config = global.kuzzle.config.server.logs;
const deprecatedConfig =
global.kuzzle.config.plugins["kuzzle-plugin-logger"];

const transport = pino.transport({
targets: [
{
target: "pino-elasticsearch",
options: {
index: "&platform.logs",
node: "http://localhost:9200",
esVersion: 7,
flushBytes: 1000,
},
},
],
});

const pinoConfigEcs = ecsFormat();

this.pino = pino<"silly" | "verbose">(
{
...pinoConfigEcs,
customLevels: {
silly: 5,
verbose: 25,
},
},
transport,
);

this.pino.setBindings({
_kuzzle_info: {
author: -1,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
updater: -1,
},
});

this.failsafeModeString = global.kuzzle.config.plugins.common.failsafeMode
? "[FAILSAFE MODE] "
: "";

if (deprecatedConfig) {
this.warn(
"[DEPRECATED] The plugins.kuzzle-plugin-logger configuration is deprecated, use server.logs instead.",
);
}
}

/**
* Logs a message with the "trace" level.
*
* @param message - The message to log.
*/
trace(message: string) {
this.pino.trace(this.formatMessage(message));
}

/**
* Logs a message with the "debug" level.
*
* @param message - The message to log.
*/
debug(message: string) {
this.pino.debug(this.formatMessage(message));
}

/**
* Logs a message with the "info" level.
*
* @param message - The message to log.
*/
info(message: string) {
this.pino.info(this.formatMessage(message));
}

/**
* Logs a message with the "warn" level.
*
* @param message - The message to log.
*/
warn(message: string) {
this.pino.warn(this.formatMessage(message));
}

/**
* Logs a message with the "error" level.
*
* @param message - The message to log.
*/
error(message: string) {
this.pino.error(this.formatMessage(message));
}

/**
* Logs a message with the "fatal" level.
*
* @param message - The message to log.
*/
fatal(message: string) {
this.pino.fatal(this.formatMessage(message));
}

/**
* Logs a message with the "silly" level.
*
* @deprecated Use Logger.trace instead.
* @param message - The message to log.
*/
silly(message: string) {
this.pino.warn(
"[DEPRECATED] Logger.silly is deprecated, use Logger.trace instead.",
);
this.pino.silly(this.formatMessage(message));
}

/**
* Logs a message with the "verbose" level.
*
* @deprecated Use Logger.debug instead.
* @param message - The message to log.
*/
verbose(message: string) {
this.pino.warn(
"[DEPRECATED] Logger.verbose is deprecated, use Logger.debug instead.",
);
this.pino.verbose(this.formatMessage(message));
}

private formatMessage(message: string): string {
const nodeIdString = global.kuzzle.id ? `[${global.kuzzle.id}] ` : "";
let requestIdString = "";

if (
global.kuzzle.asyncStore?.exists() &&
global.kuzzle.asyncStore?.has("REQUEST")
) {
const request = global.kuzzle.asyncStore.get("REQUEST");

requestIdString = `[${request.id}] `;
}
return `${nodeIdString}${this.failsafeModeString}${requestIdString}${message}`;
}
}
2 changes: 1 addition & 1 deletion lib/kuzzle/kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Router from "../core/network/router";
import Statistics from "../core/statistics";
import { TokenManager } from "../core/auth/tokenManager";
import Validation from "../core/validation";
import Logger from "./log";
import { Logger } from "./Logger";
import vault from "./vault";
import DumpGenerator from "./dumpGenerator";
import AsyncStore from "../util/asyncStore";
Expand Down
74 changes: 0 additions & 74 deletions lib/kuzzle/log.js

This file was deleted.

15 changes: 4 additions & 11 deletions lib/types/config/PluginsConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type PluginsConfiguration = {
* Edit this list to deactivate one or more of those plugins.
* NOTE: this list does not control plugins installed manually.
*
* @default ["kuzzle-plugin-logger","kuzzle-plugin-auth-passport-local"]
* @default ["kuzzle-plugin-auth-passport-local"]
*/
include: string[];

Expand Down Expand Up @@ -76,23 +76,16 @@ export type PluginsConfiguration = {
};

/**
* Default logger plugin configuration.
*
* This plugin use Winston to transport the logs.
*
* @see https://github.com/kuzzleio/kuzzle-plugin-logger
* Logger plugin configuration.
* @deprecated use server.logs
*/
"kuzzle-plugin-logger": {
/**
* Winston transport services declaration
* Services declaration
*/
services: {
/**
* Print logs to STDOUT
*
* @default
*
* @see https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport
*/
stdout: {
/**
Expand Down
8 changes: 4 additions & 4 deletions lib/types/config/ServerConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ export type ServerConfiguration = {
port: number;

/**
* Configuration section for Kuzzle access logs.
* Configuration section for Kuzzle logs.
*/
logs: {
/**
* An array of Winston transports configurations to output access
* logs.
* An array of Pino transports configurations to output logs.
*
* Possible transport types are: console, file, elasticsearch and syslog.
*
* Please refer to https://github.com/winstonjs/winston/blob/master/docs/transports.md
* Please refer to https://getpino.io/#/docs/transports?id=known-transports
* for more information on transports configuration.
*
* @default
Expand All @@ -55,6 +54,7 @@ export type ServerConfiguration = {
* be consumed by logstash agent.
*
* @default "combined"
* @deprecated use the "pino-clf" to get combined logs or "pino-socket" to send logs to logstash.
*/
accessLogFormat: "combined" | "logstash";

Expand Down
10 changes: 3 additions & 7 deletions test/core/backend/Backend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ describe("Backend", () => {

should(options.secretsFile).be.eql(application._secretsFile);
should(options.vaultKey).be.eql(application._vaultKey);
should(options.plugins).have.keys(
"kuzzle-plugin-logger",
"kuzzle-plugin-auth-passport-local",
);
should(options.plugins).have.keys("kuzzle-plugin-auth-passport-local");
should(options.installations).be.eql(
application._installationsWaitingList,
);
Expand All @@ -129,7 +126,7 @@ describe("Backend", () => {
);

application.config.content.plugins.common.include = [
"kuzzle-plugin-logger",
"kuzzle-plugin-auth-passport-local",
];

await application.start();
Expand All @@ -138,8 +135,7 @@ describe("Backend", () => {

const [, options] = global.kuzzle.start.getCall(0).args;

should(options.plugins).have.keys("kuzzle-plugin-logger");
should(options.plugins).not.have.keys(
should(options.plugins).have.only.keys(
"kuzzle-plugin-auth-passport-local",
);
});
Expand Down
Loading

0 comments on commit 8de9705

Please sign in to comment.