Skip to content

Commit b0773e3

Browse files
committed
Adding syslog client for backend logs
1 parent d6b7895 commit b0773e3

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ ADMIN_WALLET_PRIVATE_KEY='YOUR_ADMIN_WALLET_PRIVATE_KEY'
355355
# Sets how many times passwords are hashed. Higher values mean stronger security but slower processing
356356
CRYPTION_SALT=10
357357
358+
# Syslog Server Configuration
359+
SYSLOG_SERVER_ENABLED='True'
360+
SYSLOG_SERVER_HOST='192.168.1.100'
361+
SYSLOG_SERVER_PORT=514
362+
SYSLOG_SERVER_LEVEL=3
363+
SYSLOG_SERVER_USERNAME=''
364+
SYSLOG_SERVER_PASSWORD=''
365+
358366
# Server Configuration
359367
HOST_PROTOCOL='https://'
360368
HOST_PORT='6000'

backend/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"secure-random-password": "^0.2.3",
6262
"socket.io": "^4.7.5",
6363
"swagger-ui-express": "^4.4.0",
64+
"syslog-client": "^1.1.1",
6465
"uuid": "^10.0.0",
6566
"vm": "^0.1.0",
6667
"websocket-stream": "^5.5.2",

backend/src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { UtilityModule } from './modules/utility/utility.module';
2323
import { MediaModule } from './modules/media/media.module';
2424
import { AdminModule } from './modules/admin/admin.module';
2525
import { BuildingModule } from './modules/building/building.module';
26+
import { SyslogModule } from './modules/logging/syslog.module';
2627
//import { ScheduleModule } from '@nestjs/schedule';
2728

2829
@Module({
@@ -47,6 +48,7 @@ import { BuildingModule } from './modules/building/building.module';
4748
serveRoot: '/app/uploads/*',
4849
}),
4950
AuthenticationModule,
51+
SyslogModule,
5052
UserModule,
5153
BrokerModule,
5254
forwardRef(() => DeviceModule),

backend/src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ import { Inject, Logger } from '@nestjs/common';
88
import { TestService } from './modules/broker/services/test.service';
99
import { MqttLogService } from './modules/broker/services/mqtt-log.service';
1010
import { readFileSync } from 'fs';
11+
import { SyslogService } from './modules/logging/syslog.service';
1112

1213
async function bootstrap() {
1314
const app = await NestFactory.create<NestExpressApplication>(AppModule);
1415

16+
const syslog = app.get(SyslogService);
17+
18+
console.log = (...args) => {
19+
const msg = args
20+
.map((a) => (typeof a === 'string' ? a : JSON.stringify(a)))
21+
.join(' ');
22+
syslog.log(msg);
23+
};
24+
1525
const config = new DocumentBuilder()
1626
.setTitle('FidesInnova')
1727
.setDescription('The FidesInnova API description')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from '@nestjs/common';
2+
import { SyslogService } from './syslog.service';
3+
4+
@Module({
5+
imports: [],
6+
controllers: [],
7+
providers: [SyslogService],
8+
exports: [SyslogService],
9+
})
10+
export class SyslogModule {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Injectable } from '@nestjs/common';
2+
import * as Syslog from 'syslog-client';
3+
4+
@Injectable()
5+
export class SyslogService {
6+
private client: Syslog.Client | null = null;
7+
private enabled = false;
8+
private username: string;
9+
private password: string;
10+
11+
constructor() {
12+
this.enabled = process.env.Syslog_Server_Enabled === 'True';
13+
this.username = process.env.Syslog_Server_Username || '';
14+
this.password = process.env.Syslog_Server_Password || '';
15+
16+
if (this.enabled) {
17+
this.client = Syslog.createClient(process.env.Syslog_Server_Host, {
18+
port: Number(process.env.Syslog_Server_Port || 514),
19+
transport: Syslog.Transport.Udp,
20+
// auth: {
21+
// username: this.username,
22+
// password: this.password,
23+
// },
24+
});
25+
}
26+
}
27+
28+
log(message: string) {
29+
if (!this.client || !this.enabled) return;
30+
31+
const messageWithUser =
32+
this.username && this.password
33+
? `[${this.username}] ${message}`
34+
: message;
35+
36+
this.client.log(
37+
messageWithUser,
38+
{
39+
facility: Syslog.Facility.Local0,
40+
severity: Number(process.env.Syslog_Serverl_Level || 3),
41+
},
42+
(err) => {
43+
if (err) console.error('Syslog send error:', err);
44+
},
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)