Created
November 2, 2012 06:03
-
-
Save spod/3998979 to your computer and use it in GitHub Desktop.
syslog blah
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| A quick demo program to show Syslog facilities | |
| Send "typical" log messages to facility local0. | |
| Send "special" log messages to facility local3. | |
| Sample syslog-ng config extract to go with this: | |
| # This is not a complete syslog-ng config, just additions | |
| # needed for a quick test. If your system has a different | |
| # syslog daemon check docs, syslog-ng syntax is special. | |
| ### Temp for syslog example | |
| # setup log file destinations | |
| destination d_regular { file("/var/log/local0.log"); }; | |
| destination d_special { file("/var/log/local3.log"); }; | |
| # filters so we can route facilities we care about | |
| filter f_regular { facility(local0); }; | |
| filter f_special { facility(local3); }; | |
| # based on filters route log messages to destinations we care about | |
| log { source(src); filter(f_regular); destination(d_regular); }; | |
| log { source(src); filter(f_special); destination(d_special); }; | |
| ### End Temp for syslog example | |
| Running test: | |
| - update syslog-ng config with above | |
| - restart syslog-ng | |
| - test syslog-ng config | |
| $ logger -p local0.info "Hello world! regular info ..." | |
| $ logger -p local3.info "Hello world! special info ..." | |
| $ tail /var/log/local0.log /var/log/local3.log | |
| ==> /var/log/local0.log <== | |
| Nov 1 22:54:26 neckbeard michael: Hello world! regular info ... | |
| ==> /var/log/local3.log <== | |
| Nov 1 22:54:28 neckbeard michael: Hello world! special info ... | |
| - build and run this short sample | |
| $ make test | |
| $ ./test | |
| - check logs | |
| $ tail /var/log/local0.log /var/log/local3.log | |
| ==> /var/log/local0.log <== | |
| Nov 1 22:54:26 neckbeard michael: Hello world! regular info ... | |
| Nov 1 22:55:54 neckbeard ./test[32647]: log at info to local0 | |
| Nov 1 22:55:54 neckbeard ./test[32647]: log at warn to local0 | |
| Nov 1 22:55:54 neckbeard ./test[32647]: log at error to local0 | |
| ==> /var/log/local3.log <== | |
| Nov 1 22:54:28 neckbeard michael: Hello world! special info ... | |
| Nov 1 22:55:54 neckbeard ./test[32647]: special log message to local3 | |
| - celebrate! | |
| References, see: | |
| man 3 syslog - syslog functions in linux programmers manual | |
| man 5 syslog-ng.conf - man page for syslog-ng | |
| http://tools.ietf.org/html/rfc5424 - latest syslog rfc | |
| */ | |
| #include <syslog.h> | |
| #include <stdio.h> | |
| int main(int argc, char *argv[]) | |
| { | |
| // open "regular" log | |
| openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_LOCAL0); | |
| puts("logging to 'regular' facility LOCAL0"); | |
| syslog(LOG_INFO, "log at info to local0"); | |
| syslog(LOG_WARNING, "log at warn to local0"); | |
| syslog(LOG_ERR, "log at error to local0"); | |
| puts("logging to 'special' facility LOCAL3"); | |
| syslog(LOG_INFO|LOG_LOCAL3, "special log message to local3"); | |
| // close "regular" log | |
| closelog(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment