Skip to content

Commit

Permalink
Syslog support
Browse files Browse the repository at this point in the history
Resolve #5355
  • Loading branch information
syuilo committed Aug 29, 2019
1 parent 1f890c5 commit 50abb51
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type Source = {

deliverJobConcurrency?: number;
inboxJobConcurrency?: number;

syslog: {
host: string;
port: number;
};
};

/**
Expand Down
52 changes: 41 additions & 11 deletions src/services/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

This comment has been minimized.

Copy link
@acid-chicken

acid-chicken Aug 30, 2019

Member

🤔 #3713


type Domain = {
name: string;
Expand All @@ -18,13 +21,28 @@ 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 = {
name: domain,
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 {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 50abb51

Please sign in to comment.