From 50abb51ecec5a147326717146c947a9756570bcf Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 30 Aug 2019 08:29:46 +0900 Subject: [PATCH] Syslog support Resolve #5355 --- .config/example.yml | 5 ++++ package.json | 1 + src/config/types.ts | 5 ++++ src/services/logger.ts | 52 +++++++++++++++++++++++++++++++++--------- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/.config/example.yml b/.config/example.yml index e23ab126c6..0a62d5cebd 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -125,3 +125,8 @@ autoAdmin: true # IP address family used for outgoing request (ipv4, ipv6 or dual) #outgoingAddressFamily: ipv4 + +# Syslog option +#syslog: +# host: localhost +# port: 514 diff --git a/package.json b/package.json index dc27f90b91..528440fd2a 100644 --- a/package.json +++ b/package.json @@ -219,6 +219,7 @@ "stylus": "0.54.5", "stylus-loader": "3.0.2", "summaly": "2.3.0", + "syslog-pro": "1.0.0", "systeminformation": "4.14.4", "syuilo-password-strength": "0.0.1", "terser-webpack-plugin": "1.4.1", diff --git a/src/config/types.ts b/src/config/types.ts index 18382d7435..fbb348fc02 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -45,6 +45,11 @@ export type Source = { deliverJobConcurrency?: number; inboxJobConcurrency?: number; + + syslog: { + host: string; + port: number; + }; }; /** diff --git a/src/services/logger.ts b/src/services/logger.ts index c393f017c2..b170be780a 100644 --- a/src/services/logger.ts +++ b/src/services/logger.ts @@ -6,6 +6,9 @@ import { program } from '../argv'; import { getRepository } from 'typeorm'; import { Log } from '../models/entities/log'; import { genId } from '../misc/gen-id'; +import config from '../config'; + +const SyslogPro = require('syslog-pro'); type Domain = { name: string; @@ -18,6 +21,7 @@ export default class Logger { private domain: Domain; private parentLogger: Logger | null = null; private store: boolean; + private syslogClient: any | null = null; constructor(domain: string, color?: string, store = true) { this.domain = { @@ -25,6 +29,20 @@ export default class Logger { color: color, }; this.store = store; + + if (config.syslog) { + this.syslogClient = new SyslogPro.RFC5424({ + applacationName: 'Misskey', + timestamp: true, + encludeStructuredData: true, + color: true, + extendedColor: true, + server: { + target: config.syslog.host, + port: config.syslog.port, + } + }); + } } public createSubLogger(domain: string, color?: string, store = true): Logger { @@ -66,17 +84,29 @@ export default class Logger { console.log(important ? chalk.bold(log) : log); if (store) { - const Logs = getRepository(Log); - Logs.insert({ - id: genId(), - createdAt: new Date(), - machine: os.hostname(), - worker: worker.toString(), - domain: [this.domain].concat(subDomains).map(d => d.name), - level: level, - message: message, - data: data, - } as Log); + if (this.syslogClient) { + const send = + level === 'error' ? this.syslogClient.error : + level === 'warning' ? this.syslogClient.warning : + level === 'success' ? this.syslogClient.info : + level === 'debug' ? this.syslogClient.info : + level === 'info' ? this.syslogClient.info : + null as never; + + send(message); + } else { + const Logs = getRepository(Log); + Logs.insert({ + id: genId(), + createdAt: new Date(), + machine: os.hostname(), + worker: worker.toString(), + domain: [this.domain].concat(subDomains).map(d => d.name), + level: level, + message: message, + data: data, + } as Log); + } } }