UsingSyslog: Difference between revisions

From ArmadeusWiki
(New page: ==Calling syslog method from your C source code== This part is quiet simple, but we need to correctly use each parameter. First step is to declare us as syslog client : <source lang="C"> ....)
 
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= Basis =
==Calling syslog method from shell==
To do that, use '''logger''' command :
<pre class="target">
# logger -t "MYTAGNAME" "I like otters with cream"
</pre>
You can then see the message in /var/log/messages:
<pre class="target">
# tail /var/log/messages
...
Oct 16 16:50:13 armadeus user.notice MYTAGNAME: I like otters with cream
</pre>
==Calling syslog method from your C source code==
==Calling syslog method from your C source code==
This part is quiet simple, but we need to correctly use each parameter.
This part is quite simple, but we need to correctly use each parameter.
First step is to declare us as syslog client :
First step is to declare us as syslog client :
<source lang="C">
<source lang="C">
Line 21: Line 35:
     ...
     ...
</source>
</source>
if you compile the source file:
If you compile the source file:
<source lang="C">
<source lang="C">
// log_syslog.c
// log_syslog.c
Line 28: Line 42:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
openlog("program_name", LOG_PID, LOG_USER);
    openlog("program_name", LOG_PID, LOG_USER);
syslog(LOG_INFO, "%s called with %d arguments", argv[0], argc - 1);
    syslog(LOG_INFO, "%s called with %d arguments", argv[0], argc - 1);
closelog();
    closelog();
 
    return 0;
return 0;
}
}
</source>
</source>
Line 44: Line 57:
itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum
itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum
</pre>
</pre>
You will probably obtain no result in shell, but you can take a look in you're system log (/var/log/syslog or /var/log/messages)
You will probably obtain no result in shell, but you can take a look in your system log (/var/log/syslog or /var/log/messages)
<pre class="apf">
<pre class="apf">
itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog
itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog
Line 51: Line 64:
</pre>
</pre>
We can notice, that the lines contain program_name[PID] pattern showing us the name we have specified in openlog and the pid of the process because we ask it with LOG_PID argument.
We can notice, that the lines contain program_name[PID] pattern showing us the name we have specified in openlog and the pid of the process because we ask it with LOG_PID argument.
= Packages =
== Busybox ==
By default, syslog is managed by busybox, it can be configured using menuconfig :
<pre class="host">
$ make busybox-menuconfig
</pre>
The configuration menu can be found here :
<pre class="config">
System Logging Utilities  --->
</pre>
== Buildroot packages ==
Some other packages are available under buildroot. To see it launch buildroot menuconfig :
<pre class="host">
$ make menuconfig
</pre>
Select option :
<pre class="config">
Package Selection for the target  --->
    [*]  Show packages that are also provided by busybox
</pre>
Then go to following menu and choose your prefered syslog implementation ;-):
<pre class="config">
Target packages  --->
    System tools  --->
        ...
        [ ] rsyslog
        ...
        [ ] sysklogd
        ...
        [ ] syslog-ng
        ...
</pre>
== Tips ==
* By default logs are not persistent (/var/log points to /tmp). To have persistent logs with rotation every 1Mbytes, one can try to add this to his post_build.sh script:
<source lang="bash">
# for persistent and rotating log messages
rm -rf $1/var/log
mkdir $1/var/log
sed -i -e 's/SYSLOGD_ARGS=""/SYSLOGD_ARGS="-s 1024 -b 10"/' $1/etc/init.d/S01syslogd
</source>

Latest revision as of 14:52, 10 October 2025

Basis

Calling syslog method from shell

To do that, use logger command :

# logger -t "MYTAGNAME" "I like otters with cream"

You can then see the message in /var/log/messages:

# tail /var/log/messages 
...
Oct 16 16:50:13 armadeus user.notice MYTAGNAME: I like otters with cream

Calling syslog method from your C source code

This part is quite simple, but we need to correctly use each parameter. First step is to declare us as syslog client :

...
#include <syslog.h>

int main(int argc, char *argv[])
{
    ...
    openlog("program_name", LOG_PID, LOG_USER);
    ...
    closelog();
    ...
}

Then, you can call syslog from your code using printf like format :

    ...
    syslog(LOG_INFO, "%s called with %d arguments\n", argv[0], argc);
    ...

If you compile the source file:

// log_syslog.c
#include <syslog.h>

int main(int argc, char *argv[])
{
    openlog("program_name", LOG_PID, LOG_USER);
    syslog(LOG_INFO, "%s called with %d arguments", argv[0], argc - 1);
    closelog();
    return 0;
}

With the command :

itsme@mycomputer:~/log_syslog$ gcc log_syslog.c -o log-syslog

Then run it 2 times :

itsme@mycomputer:~/log_syslog$ ./log_syslog
itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum

You will probably obtain no result in shell, but you can take a look in your system log (/var/log/syslog or /var/log/messages)

itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog
Sep 19 18:46:49 mycomputer program_name[3022]: ./log_syslog called with 0 arguments
Sep 19 18:46:53 mycomputer program_name[3023]: ./log_syslog called with 3 arguments

We can notice, that the lines contain program_name[PID] pattern showing us the name we have specified in openlog and the pid of the process because we ask it with LOG_PID argument.

Packages

Busybox

By default, syslog is managed by busybox, it can be configured using menuconfig :

$ make busybox-menuconfig

The configuration menu can be found here :

System Logging Utilities  --->

Buildroot packages

Some other packages are available under buildroot. To see it launch buildroot menuconfig :

$ make menuconfig

Select option :

Package Selection for the target  --->
    [*]   Show packages that are also provided by busybox

Then go to following menu and choose your prefered syslog implementation ;-):

Target packages  --->
    System tools  --->
        ...
        [ ] rsyslog
        ...
        [ ] sysklogd
        ...
        [ ] syslog-ng
        ...

Tips

  • By default logs are not persistent (/var/log points to /tmp). To have persistent logs with rotation every 1Mbytes, one can try to add this to his post_build.sh script:
# for persistent and rotating log messages
rm -rf $1/var/log
mkdir $1/var/log
sed -i -e 's/SYSLOGD_ARGS=""/SYSLOGD_ARGS="-s 1024 -b 10"/' $1/etc/init.d/S01syslogd