logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Erskine <michael.ersk...@ketech.com>
Subject RE: Distributed Logging Problem (log to remote database possible?)
Date Fri, 08 Aug 2008 08:04:07 GMT
CheapLisa [mailto:lisa@purpleblade.net] wrote:
> I want to log information on the hub and the six machines in one place (a
> database residing on yet another machine).
> Is this possible to do?

Sure, this is a rather common requirement. You can detach the database logging entirely by
configuring a org.apache.log4j.net.SocketHubAppender on each of your six machines then creating
a simple socket client that connects to each machine and receives all the LoggingEvent objects
which it can write to any database at its leisure (preferably asynchronously in a background
thread with a BlockingQueue or somesuch).

Just for a starter hint the very simplest client to connect to a single remote SocketHubAppender
and log the messages locally would look like this...

---------------------- >8 --- CUT HERE --- >8 --------------------

public class RemoteLoggingClient {
        static Logger log = Logger.getLogger(RemoteLoggingClient.class);
         * @param args
        public static void main(String[] args) {
                // connect to remote server that is logging with log4j
                try {
                        Socket s = new Socket("whateverserver", 9009);
                        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                        while (true) {
                                LoggingEvent event = (LoggingEvent) ois.readObject();
                                log.debug("remote event:'" + event.getMessage() + "'");
                } catch (UnknownHostException e1) {
                        log.fatal("problem:", e1);
                } catch (IOException e1) {
                        log.fatal("problem:", e1);
                } catch (ClassNotFoundException e) {
                        log.fatal("problem:", e);
---------------------- >8 --- CUT HERE --- >8 --------------------

You can scale such a thing up to connect to many servers, keep the connections up, render
the logged events, queue them for writing to the databases, write to the databases asynchronously
with DB connection pooling and recovery, etc., etc. Lots of scope for fun here!

Michael Erskine.

To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message