Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I successfully tested the following syslog "hello world" example on ubuntu 12.04:

// gcc giuspexample.c -o giuspexample

#include <syslog.h>

int  main(int argc, char *argv[])
    setlogmask(LOG_UPTO (LOG_NOTICE));

    openlog("atm", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);

    syslog(LOG_NOTICE, "Program started by User %d", getuid ());
    syslog(LOG_INFO, "A tree falls in a forest");

    return 0;

and I can read the entry in /var/log/syslog. I followed the instructions on http://www.codealias.info/technotes/syslog_simple_example on how to change the destination filepath

echo "local0.*  /var/log/mylog" >> /etc/syslog.conf

but trying to run

sudo /etc/init.d/syslog restart

doesn't work (command not found) and rebooting the pc anyway doesn't start to write on /var/log/mylog but still on /var/log/syslog. Does anybody know what's wrong? Thanks.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Ok, here's how I succeeded.

In my folder /etc/rsyslog.d there are two files:

20-ufw.conf and 50-default.conf

I added a file:

sudo nano /etc/rsyslog.d/30-mycustomname.conf

With the following content:

# Log QSD Centro generated log messages to file
if $programname == 'centro' then /var/log/centro.log
# Uncomment the following to stop logging anything that matches the last rule.
& ~

Then I check that the file /var/log/centro.log does not exist

sudo rm -f /var/log/centro.log

Then I restart the service

sudo service rsyslog restart

Finally The following code works:

// gcc centro.c -o centro

#include <stdio.h>
#include <syslog.h>

int  main(int argc, char *argv[])
    openlog(NULL, 0, LOG_USER);

    syslog(LOG_INFO, "MORTACCI TUA!!!");

    return 0;

Hope this helps.

share|improve this answer
Works great also on Fedora 16. –  user19862 Jun 14 '12 at 22:26
+1 nice trick with the 30- prefix to control ordering! –  kfmfe04 May 29 '13 at 2:28
Nice one. This fixed my issue of ordering that was driving me crazy. –  dmourati Oct 23 '13 at 19:02
add comment

By default Ubuntu uses rsyslog. It's configuration files are in



share|improve this answer
Thanks, I tried to add the line "local0.* /var/log/mylog" at the end of /etc/sysctl.conf but after rebooting the pc still doesn't log to /var/log/mylog –  giuspen Jun 11 '12 at 16:23
are you using syslog-ng ? I am running Precise. "sudo apt-cache search syslog" returns syslog-ng but not syslog. You may have to install that first. –  RedMage Jun 11 '12 at 17:23
If I mark for installation syslog-ng in synaptic he forces me to remove rsyslog. It seems to me rsyslog is used. I tired also to use the config file /etc/rsyslog.d/50-default.conf but my option "local0.* /var/log/mylog" was ignore there too –  giuspen Jun 11 '12 at 17:29
OK. That seems to be the case on my system as well. /etc/rsyslog.conf is the conf file for that. This explains why sudo /etc/init.d/syslog restart did not work. It's rsyslog and not plain old syslog. man rsyslog and man /etc/rsyslog.conf will help you out. It says: " For more information see /usr/share/doc/rsyslog‐doc/html/rsys‐log_conf.html # # Default logging rules can be found in /etc/rsyslog.d/50‐default.conf –  RedMage Jun 11 '12 at 17:33
I did write to /etc/rsyslog.d/50-default.conf but with no result. I started to doubt that my C code is not correct for rsyslog but found no example specific for rsyslog and no other header other than syslog.h on my installation. Tried more filters posted in different forums for rsyslog and custom log file but nothing worked :( –  giuspen Jun 11 '12 at 17:52
show 2 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.