Skip to content

Commit

Permalink
Logging tweaks
Browse files Browse the repository at this point in the history
- json logs now report log level
- debug logs are not writen to a file but to stdout
  • Loading branch information
daneryl committed Jul 8, 2024
1 parent 2dc2bbc commit c878426
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/api/log/debugLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const createDebugLog = () => {

return winston.createLogger({
transports: [
new winston.transports.File({
filename: `${LOGS_DIR}/debug.log`,
new winston.transports.Console({
handleExceptions: true,
level: 'debug',
format: formatter(DATABASE_NAME),
}),
Expand Down
14 changes: 1 addition & 13 deletions app/api/log/errorLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,12 @@ const createConsoleTransport = () =>
format: formatter(DATABASE_NAME),
});

const createJSONConsoleTransport = () =>
new winston.transports.Console({
handleExceptions: true,
level: 'error',
format: jsonFormatter(DATABASE_NAME),
});

const createErrorLog = () => {
DATABASE_NAME = process.env.DATABASE_NAME ? process.env.DATABASE_NAME : 'localhost';
LOGS_DIR = process.env.LOGS_DIR ? process.env.LOGS_DIR : './log';

let transports = [createFileTransport(), createConsoleTransport()];
if (config.JSON_LOGS) {
transports = [createJSONConsoleTransport()];
}

const logger: ExtendedLogger = winston.createLogger({
transports,
transports: [createFileTransport(), createConsoleTransport()],
});

logger.closeGraylog = (cb = () => {}) => {
Expand Down
13 changes: 8 additions & 5 deletions app/api/log/infoFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const addTenant = (info: any, { instanceName }: { instanceName: string }) => {

const formatInfo = (info: any) => {
const message = info.message && info.message.join ? info.message.join('\n') : info.message;
return `${info.timestamp} [${info.tenant}] ${message}${
info.tenantError ? `\n[Tenant error] ${info.tenantError}` : ''
}`;
return `${info.timestamp} [${info.tenant}] ${message}${info.tenantError ? `\n[Tenant error] ${info.tenantError}` : ''}`;
};

const jsonFormatter = (DATABASE_NAME: String) =>
Expand All @@ -31,6 +29,7 @@ const jsonFormatter = (DATABASE_NAME: String) =>
winston.format.printf(info =>
JSON.stringify({
application_name: 'Uwazi',
level: info.level,
timestamp: info.timestamp,
environment: config.ENVIRONMENT,
tenant: info.tenant,
Expand All @@ -40,11 +39,15 @@ const jsonFormatter = (DATABASE_NAME: String) =>
)
);

const formatter = (DATABASE_NAME: String) =>
winston.format.combine(
const formatter = (DATABASE_NAME: String) => {
if (config.JSON_LOGS) {
return jsonFormatter(DATABASE_NAME);
}
return winston.format.combine(
winston.format.timestamp(),
winston.format(addTenant)({ instanceName: DATABASE_NAME }),
winston.format.printf(info => formatInfo(info))
);
};

export { jsonFormatter, formatter, addTenant, formatInfo };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.175.0-rc1",
"version": "1.175.0-rc2",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit c878426

Please sign in to comment.