[LOGBack-dev] svn commit: r419 - in logback/classic/trunk/src/main/java/ch/qos/logback/classic: net pattern util

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Aug 8 23:13:06 CEST 2006


Author: ceki
Date: Tue Aug  8 23:13:05 2006
New Revision: 419

Added:
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java
Log:

- on going work on SyslogAppender
- added support for instance converter map in PatternLayout
- various copyright related changes (minor)

Added: logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java	Tue Aug  8 23:13:05 2006
@@ -0,0 +1,64 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.classic.net;
+
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.classic.util.LevelToSyslogSeverity;
+import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.net.SyslogAppenderBase;
+
+/**
+ * 
+ * @author Ceki G&uumllcü
+ */
+public class SyslogAppender extends SyslogAppenderBase {
+
+  Layout layout;
+  
+  public Layout makeDefaultLayout(int facility) {
+    PatternLayout pl = new PatternLayout();
+    pl.getInstanceConverterMap().put("syslogStart", "sfsdf");
+    pl.setPattern("%syslogStart{"+facility+"} ");
+    pl.setContext(getContext());
+    pl.start();
+    return pl;
+  }
+  
+  /*
+   * Convert a level to equivalent syslog severity. Only levels for printing methods
+   * i.e DEBUG, WARN, INFO and ERROR are converted.
+   * 
+   * @see ch.qos.logback.core.net.SyslogAppenderBase#getSeverityForEvent(java.lang.Object)
+   */
+  @Override
+  public int getSeverityForEvent(Object eventObject) {
+    LoggingEvent event = (LoggingEvent) eventObject;
+    return LevelToSyslogSeverity.convert(event);
+  }
+
+  /*
+   * Set the layout.
+   * 
+   * @see ch.qos.logback.core.Appender#setLayout(ch.qos.logback.core.Layout)
+   */
+  public void setLayout(Layout layout) {
+    this.layout = layout;
+  }
+
+  /*
+   * 
+   * @see ch.qos.logback.core.Appender#getLayout()
+   */
+  public Layout getLayout() {
+    return layout;
+  }
+
+}

Added: logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java	Tue Aug  8 23:13:05 2006
@@ -0,0 +1,102 @@
+/**
+ * LOGBack: the reliable, fast and flexible logging library for Java.
+ *
+ * Copyright (C) 1999-2006, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.pattern;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.TimeZone;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.classic.util.LevelToSyslogSeverity;
+import ch.qos.logback.core.CoreGlobal;
+
+public class SyslogStartConverter extends ClassicConverter {
+
+  long lastTimestamp = -1;
+  String timesmapStr = null;
+  SimpleDateFormat simpleFormat = null;
+  String localHostName;
+  int facility;
+
+  public void start() {
+
+    String datePattern = getFirstOption();
+    if (datePattern == null) {
+      datePattern = CoreGlobal.ISO8601_PATTERN;
+    }
+
+    localHostName = getLocalHostname();
+    try {
+      simpleFormat = new SimpleDateFormat(datePattern);
+      // maximumCacheValidity =
+      // CachedDateFormat.getMaximumCacheValidity(pattern);
+    } catch (IllegalArgumentException e) {
+      getLogger().warn(
+          "Could not instantiate SimpleDateFormat with pattern " + datePattern,
+          e);
+      // default to the ISO8601 format
+      simpleFormat = new SimpleDateFormat(CoreGlobal.ISO8601_PATTERN);
+    }
+
+    List optionList = getOptionList();
+
+    // if the option list contains a TZ option, then set it.
+    if (optionList != null && optionList.size() > 1) {
+      TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
+      simpleFormat.setTimeZone(tz);
+    }
+  }
+
+  public String convert(Object event) {
+    LoggingEvent le = (LoggingEvent) event;
+    StringBuilder sb = new StringBuilder();
+
+    int pri = facility + LevelToSyslogSeverity.convert(le);
+    System.out.println("" + pri);
+    sb.append("<");
+    sb.append(pri);
+    sb.append(">");
+    fillInTimestamp(sb, le.getTimeStamp());
+    sb.append(' ');
+    sb.append(localHostName);
+    sb.append(' ');
+
+    return sb.toString();
+  }
+
+  /**
+   * This method gets the network name of the machine we are running on.
+   * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name 
+   * cannot be found.
+   * @return String the name of the local host
+   */
+  public String getLocalHostname() {
+    try {
+      InetAddress addr = InetAddress.getLocalHost();
+      return addr.getHostName();
+    } catch (UnknownHostException uhe) {
+      addError("Could not determine local host name", uhe);
+      return "UNKNOWN_LOCALHOST";
+    }
+  }
+
+  void fillInTimestamp(StringBuilder sb, long timestamp) {
+    // if called multiple times within the same millisecond
+    // use last value
+    if (timestamp != lastTimestamp) {
+      lastTimestamp = timestamp;
+      timesmapStr = simpleFormat.format(new Date(timestamp));
+    }
+    sb.append(timesmapStr);
+  }
+}

Added: logback/classic/trunk/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java	Tue Aug  8 23:13:05 2006
@@ -0,0 +1,42 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.classic.util;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.net.SyslogConstants;
+
+public class LevelToSyslogSeverity {
+
+  /*
+   * Convert a level to equivalent syslog severity. Only levels for printing methods
+   * i.e DEBUG, WARN, INFO and ERROR are converted.
+   * 
+   */
+  static public int convert(LoggingEvent event) {
+
+    Level level = event.getLevel();
+
+    switch (level.levelInt) {
+    case Level.ERROR_INT:
+      return SyslogConstants.ERROR_SEVERITY;
+    case Level.WARN_INT:
+      return SyslogConstants.WARNING_SEVERITY;
+    case Level.INFO_INT:
+      return SyslogConstants.INFO_SEVERITY;
+    case Level.DEBUG_INT:
+      return SyslogConstants.LOG_ALERT;
+    default:
+      throw new IllegalArgumentException("Level " + level
+          + " is not a valid level for a printing method");
+    }
+  }
+}



More information about the logback-dev mailing list