I am running OSX 10.10.4 and have a cron job scheduled there (it runs python script). Log file is enabled there in accordance with this answer - https://superuser.com/a/595825/172606. But the log created contains information about cron job starts and crontab file updates only. Is there any way to put some information there from my python script? (don't want to create separate log for each python script I have)
2 Answers
I dont know about OSX but on linux we have the logger command to add log entries via syslog:
$ logger -p cron.info 'some important message'
- 6,624
The short answer to your question is, "Yes, you can do that."
As another person said, your script can use the logger utility on any Unix/Linux/BSD variant (including MacOS X) to send messages to the system logs.
WHERE it goes in the system logs usually depends on two tags on the message called the "facility" and the "severity". Together, these tags are called the "priority" of the message. (More on this later.)
USUALLY, the facility is more important for determining which log messages go to. Messages with the "mail" facility or the "cron" facility will usually go in separate log files for those purposes. There is also usually a catch-all for anything not handled explicitly (usually /var/log/syslog or /var/log/messages).
So to make your script send logs to the same logs as cron, you would use:
logger -p cron.info "What you want to say goes here."
The -p stands for "priority" which is in the form facility.severity.
You would use the cron facility, because that's what makes the message go into cron's logs.
The severity is a generic thing that tells the system logs how important this message is. The reason is that sometimes there are generic handlers for "all critical messages" or "all alarms" no matter where they come from.
Without diving too much into that, there are probably a few severities you will find useful for your script:
error- Use this when something has definitely gone wrong that someone should look at AND FIX.warning- Use this when something is suspicious, but a human would need to look at it to tell for sure.notice- Less important thanwarningbut more important than the genericinfo. I like to think of this as "bold face" and I use it for messages likeMyScript Starting at <date>orMyScript Finished at <date>since these are notable, but not at all a problem.info- Use this for most log messages.debug- Use this when you're troubleshooting the script itself, and then TURN THIS OFF when you're not using it. (Debug logging is a great way to cause trouble for yourself later.)
These severities are worth remembering because they are used almost universally by "stuff that writes logs," even in non-Unix systems.
Other severities you may encounter are:
fatal(oremergencyorpanic)alarm(oralert)critical(orcrit)trace
The first three are "worse" than error and trace is either equivalent to debug or is handled as "high-level debug." Not all logging systems handle these the same way and this list is not meant to be exhaustive.
On MacOS X, run man 3 syslog from Terminal.app to see the list again.
- 370