How to use system.log but send the log to one or more "programatically created" logs?

Hello all,

I created logging that uses text files and writes to a specific file when specific actions or errors occur. The problem with this approach is that unless I write a handler for these files, they will just grow and grow. So my question is, can system.log be used or something similar to acheieve both special separated logs dedicated to my app(s) but controlled by the system with regard to how long to maintain, how large they can become etc etc.

I looked for a sample, but could not find anything - although that would be the first time I couldn’t find a sample! Any ideas, samples direction etc would be very much appreciated. I want to do a beta, but I don’t think I should do this without having managed logging.

Thanks,
Tim

Hey Tim - one approach I use that works great on Unix systems is just to put entries into the system log using the ‘logger’ command (built into most unixes). The ‘logger’ command is also on Mac so this should work there too but I haven’t tried it. I use it mostly for web applications which I host on unix.

Here is an example of a method I use to write to the system log:

Sub logger(myLog as string, myTag as string, myLevel as String = "notice", myFacility as String = "local3")

    if TargetLinux then
		    
         dim sh as new Shell
         sh.Mode = 0
         myLog = ReplaceAll(myLog, chr(34), chr(39)) ' replace " with ' to avoid errors
         sh.Execute("logger -p " + myFacility + "." + myLevel + " -t " + myTag + chr(32) + chr(34) + myLog + chr(34))
		    
   else
		    Print(myTag + "." + myLevel + ": " + myLog)
   end
	
End Sub

You can configure rsyslogd to route the log to a different location by facility level and you can include a tag for filtering purposes. So for instance I route everything to a xojo.log file and then I use a different tag for each different application or utility that is running.

You could probably connect directly to the logging daemon but this was easier for me and has not been an issue and has been logging faithfully for about 7 different apps/utilities for over 2 years.

You can also startup your application and pipe the output directly to a log file either using systemd or upstart (on linux) or if you app is a console app you can pipe the output directly to a log file. Then you can configure ‘logrotate’ to rotate that file.

for instance if you are running a console app using cron you could put in the command like this:

/path/to/my/app &> /path/to/my/log.log

Then add a file in this directory: /etc/logrotate.d called ‘myapp’ and configure something like this:

/path/to/my/log.log {
    weekly
    rotate 10
    missingok
    notifempty
    compress
    create 0600 root root
}

‘create’ creates a new empty log file - need to be careful about permissions here. Also, you may need to restart your app depending on your setup.

These instructions are based on RHEL 7 but something similar will work on most unix situations.
If you are Windows, this won’t help at all - but the concept will be similar.