Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select server, port and different RFC #11

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules
*.log
*.log
*.pid
test/child
*.iml
.idea/**
node_modules
*.log
*.log
*.pid
test/child
*.iml
.idea/**
79 changes: 46 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
# pm2-syslog

Redirect all logs of PM2 + Apps managed into `/var/log/syslog`

## Configure OS

Edit `/etc/rsyslog.conf` and uncomment:

```
# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")
```

Restart rsyslog:

```
$ sudo service rsyslog restart
```

## Install module

```
# Install
$ pm2 install pm2-syslog

# Uninstall
$ pm2 uninstall pm2-syslog
```

# License

MIT
# pm2-syslog

This is a fork of https://github.com/pm2-hive/pm2-syslog

It adds the ability to send logs to a remote Syslog server and specify its IP and port

## Howto

```
pm2 install agrosjea/pm2-syslog
pm2 set pm2-syslog:serverAddress [xxx.xxx.xxx.xxx] (localhost by default)
pm2 set pm2-syslog:serverPort [xxxxx] (514 by default)
pm2 set pm2-syslog:syslogFormat [xxxxx] (RFC5424 by default, other format supported is RFC3164)
```

Redirect all logs of PM2 + Apps managed into `/var/log/syslog`

## Configure OS

Edit `/etc/rsyslog.conf` and uncomment:

```
# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")
```

Restart rsyslog:

```
$ sudo service rsyslog restart
```

## Install module

```
# Install
$ pm2 install pm2-syslog

# Uninstall
$ pm2 uninstall pm2-syslog
```

# License

MIT
96 changes: 72 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@

var pm2 = require('pm2');
var SysLogger = require('ain2');
var logger = new SysLogger({tag: 'pm2', facility: 'syslog'});

pm2.launchBus(function(err, bus) {
bus.on('*', function(event, data){
if (event == 'process:event') {
logger.warn('app=pm2 target_app=%s target_id=%s restart_count=%s status=%s',
data.process.name,
data.process.pm_id,
data.process.restart_time,
data.event);
}
});

bus.on('log:err', function(data) {
logger.error('app=%s id=%s line=%s', data.process.name, data.process.pm_id, data.data);
});

bus.on('log:out', function(data) {
logger.log('app=%s id=%s line=%s', data.process.name, data.process.pm_id, data.data);
});
});

var pm2 = require('pm2');
const pmx = require('pmx');
var SysLogger = require('ain2');

const conf = pmx.initModule();


var serverAddress = conf.serverAddress || "localhost"
var serverPort = conf.serverPort || "514"
var syslogFormat = conf.syslogFormat || "RFC5424"
// RFC 3164 : <133>Feb 25 14:09:07 webserver syslogd[0]: restart
// <facility> date host app pid log
// RFC 5424 : <34>1 2003-10-11T22:14:15.003Z mymachine myapplication 1234 ID47 [mydata class="high"] BOMmyapplication is started
// <facility>ver. date host app pid msgid structured data log

console.log(`Starting pm2-syslogger logging to ${serverAddress}:${serverPort} using ${syslogFormat}`)
var logger = new SysLogger({facility: 1 ,address : serverAddress});
logger.setPort(serverPort)


if (syslogFormat == "RFC3164"){
logger.setMessageComposer(function(message, severity){
return new Buffer.from('<' + (this.facility * 8 + severity) + '> ' +
this.getDate() + ' ' + this.hostname + ' ' + message);
});
} else { //default to RFC5424
logger.setMessageComposer(function(message, severity){
return new Buffer.from('<' + (this.facility * 8 + severity) + '>1 ' +
this.getDate() + ' ' + this.hostname + ' ' + message);
});
}

// ON pm2 event
pm2.launchBus(function(err, bus) {
bus.on('*', function(event, data){
if (event == 'process:event') {
logger.warn(`pm2 ${data.process.pm_id} 0 [pm2 process="${data.process.name}" restart_count="${data.process.restart_time}" status="${data.event}"]`);
}
});


// ON COUT
bus.on('log:out', function(data) {
message = data.data;
pmId = data.process.pm_id;
//retrieving pid from pm2 description
pm2.describe(pmId, function(err, processDescription){
pid = processDescription[0].pid;
if (syslogFormat == "RFC3164"){
logger.log(`${data.process.name}[${pid}]:${message}`);
} else { //default to RFC5424
logger.log(`${data.process.name} ${pid} 0 [] ${message}`);
}
});
})

// ON CERR
bus.on('log:err', function(data) {
message = data.data;
pmId = data.process.pm_id
//retrieving pid from pm2 description
pm2.describe(pmId, function(err, processDescription){
pid = processDescription[0].pid;
if (syslogFormat == "RFC3164"){
logger.error(`${data.process.name}[${pid}]:${message}`);
} else { //default to RFC5424
logger.error(`${data.process.name} ${pid} 0 [] ${message}`);
}
});
})
});
48 changes: 27 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
{
"name": "pm2-syslog",
"version": "2.1.0",
"description": "Redirect PM2/apps logs to syslog",
"main": "probe.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"ain2": "^2.0.0",
"pm2": "latest"
},
"apps": [
{
"name": "pm2-syslog",
"script": "app.js"
}
],
"author": "Alexandre Strzelewicz",
"license": "MIT"
}
{
"name": "pm2-syslog",
"version": "2.1.2",
"description": "Redirect PM2/apps logs to syslog",
"main": "probe.js",
"repository": "http://github.com/agrosjea/pm2-syslog",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"ain2": "git://github.com/phuesler/ain.git#v3.0.0",
"pm2": "3.5.0",
"pmx": "1.6.7"
},
"apps": [
{
"name": "pm2-syslog",
"script": "app.js"
}
],
"author": "Alexandre Strzelewicz",
"license": "MIT",
"config": {
"serverAddress": "localhost",
"serverPort": "514"
}
}