Background
On Linux systems, Application Logs exist in subdirectories of /var/log
, which is owned by root/root
and has 755 permissions on my system. For example, I see /var/log/mysql
and /var/log/samba
.
Question
If I want a myapp to be able to write into a /var/log/myapp
, what is the canonical way of accomplishing this in C/C++?
Thoughts
Do I have to do something crazy like setuid root
if I don't want to sudo a_setup_script.sh
? Note that I am aware of the syslog
routines, but they are insufficient for my needs (I need to log much more information, separated into different files, hence the need for the subdirectory).
Do I need to look into a combination of Ubuntu packaging (to set up the directory) and direct file IO into the subdirectory (by myapp)?
I would like to follow standards as much as possible.
Addendum
I forgot to mention, myapp is actually a daemon processes (a server that listens to clients) so it wouldn't be so bad to have a myapp_user which actually runs/starts the process.
ANSWER
For Ubuntu, the best solution appears to be rsyslog
, a powerful, modern replacement for syslog
. It will generate files/directories as necessary, it has a built-in language for flexible routing of syslog
entries, and it uses the simple, old syslog
API at the C/C++ level. To store routing information, you can define your own encoding of the text message in C/C++, in conjunction with a proper rsyslog.conf
to handle the decoding.
On a Windows computer: Inside the Control Panel, find System & Security. From there, go to Administrative Tools and then the Event Viewer. Open Windows Logs and choose Application. This will show you all the application logs saved on your computer.
The syslog service receives and processes syslog messages and listens for events by creating a socket located at /dev/log , which applications can write to. It can write messages to a local file or forward messages to a remote server. There are different syslog implementations, including rsyslogd and syslog-ng.
Set a Standard Location for Log Files Linux systems typically save their log files under the /var/log directory. This works fine, but check if the application saves under a specific directory under /var/log. If it does, great. If not, you may want to create a dedicated directory for the application under /var/log.
No, no no no. No suid for such stuff. These logs are managed by a process known as "syslog" and there is an API to send messages to this logger:
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
Or you can type 'man syslog' on the command line and get all the info :-)
Update: you will need permissions to edit syslog's configuration file to send message to a separate log file, otherwise they will end up in the default location (probably /var/log/syslog).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With