How to write to OS syslog in Java?

Mac OS has an application called Console that contains logged messages, errors and errors. I believe the Windows equivalent is Event Viewer. I would assume there is one in Linux, but I don't know anything about that or where it is.

Is it possible to get a message from Java output to syslog like this? I am writing a GUI based application so nothing works on the command line. Standard System.out

or System.err

probably won't make a lot of sense in this case, unless I miss something.

I wrote a simple logging service for my application that writes to a dedicated log file, but I want to have some kind of failover in case of an I / O error while trying to write to that file.

I know the IDE will display the output through System.out

and System.err

just fine, but this happens if the end user encounters such a problem.

As an example: I wrote "code language modules" for the TextWrangler application on the Mac. These modules are read by TW when the application starts, and when an error occurs while handling them, the errors are logged and can be viewed in the Mac Console application.

+3


source to share


2 answers


On Linux it is called syslog. One of the ways you can achieve console logging on a Mac would be using log4j 'org.apache.log4j.net.SyslogAppender'.



I think this link should give you some kickstarts in that direction.

+1


source


You can redirect System.out to a log file, and the Mac Console is the default viewer for files ending in ".log".

One way that is usually done is a shell script that will invoke your Java program. This Java 7 example redirects the output of the call to the main class MyClass to mylogfile.log. Everything written in System.out will be in mylogfile.log file.



#!/bin/bash

for a in /path/to/my/jars/*
do 
CLASSPATH=$CLASSPATH:$a
done

java -Xms128M -Xmx128M -XX:MaxPermSize=128M -cp ${CLASSPATH} com.example.package.MyClass >> mylogfile.log

      

+1


source







All Articles