On Mac OS X how can I log to Console.app from the terminal?

I've seen the use of /dev/console in several scripts for Mac OSX but I'm not sure how it's being used. I checked Console.app and didn't see the results of my test command:

echo test > /dev/console

being printed out there, so where does it go to, or how would you use it?


Solution 1:

The /dev/console device is not especially related to the Console application.

In Mac OS X 10.6, the Console application has two kinds of data sources that it reads: normal log files (e.g. the files in /var/log/), and messages logged through the (Apple extended) syslog facility.

If you have a log file that you want to monitor, you can use the File > Open… menu item (⌘O) to view the file in Console.
Note: The only way to remove a file from Console’s list of files seems to be to use its File > Move to Trash menu item (⌘⌫); be prepared to pull the file out of the Trash if you just want to stop watching it in Console.

You can use the syslog command-line program to send messages to the syslog facility.

syslog -s Your message goes here. \(quote special chars for the shell'!)'

However, under the default configuration, this message will never show up in any of the Console views because it’s “level” is too low to be important. This initial filtering is due by syslogd. It is configured by the asl.conf and syslog.conf files. The default configuration does not store most messages if their Level is too low (and syslog -s defaults to the lowest level). Console can only show message that have been stored by syslogd.

You can change the value of the Level field by using the -l option:

syslog -s -l notice This message should show up in \"All Messages\" \
  with a Facility of syslog.

The notice level is the (default) lowest level for which syslogd will store most messages.

A message generated like this will show up in Console’s built-in “All Messsages” database search
(the file All Messages.aslquery in /Applications/Utilities/Console.app/Contents/Resources/ASLQueries/ technically limits it to message where the Facility field “contains” the empty string, but this condition is trivially satisfied by any Facility value).

If you want your message to appear in Console’s built-in “Console Messages” database search, then you must be a bit more specific. It only shows (stored) messages that have a Facility field that equals com.apple.console (see the file Console Messages.aslquery in /Applications/Utilities/Console.app/Contents/Resources/ASLQueries/). You can generate such messages with syslog by the -k option to set the Facility field.

syslog -s -k Facility com.apple.console \
          -k Level notice \
          -k Message 'This will show up in "Console Messages"'

When using -k, all the options and the message itself must be specified with sets of -k key value arguments (we can not use -l to set the level). This means that we have to put the message a single argument so it can be the value of the Message field.

Solution 2:

i use:

logger Hello World

always works for me. dumps "hello world" to syslog, which should show up in console.app