
Author: ceki Date: Tue Aug 8 23:13:08 2006 New Revision: 420 Added: logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/FileAppender.java logback/core/trunk/src/main/java/ch/qos/logback/core/WriterAppender.java logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogConstants.java logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogWriter.java logback/core/trunk/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java logback/core/trunk/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java Log: - on going work on SyslogAppender - added support for instance converter map in PatternLayout - various copyright related changes (minor) Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/FileAppender.java ============================================================================== --- logback/core/trunk/src/main/java/ch/qos/logback/core/FileAppender.java (original) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/FileAppender.java Tue Aug 8 23:13:08 2006 @@ -1,11 +1,11 @@ /** - * LOGBack: the reliable, fast and flexible logging library for Java. - * + * 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. + * + * 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.core; Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/WriterAppender.java ============================================================================== --- logback/core/trunk/src/main/java/ch/qos/logback/core/WriterAppender.java (original) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/WriterAppender.java Tue Aug 8 23:13:08 2006 @@ -1,11 +1,11 @@ /** - * LOGBack: the reliable, fast and flexible logging library for Java. - * + * 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. + * + * 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.core; Added: logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java ============================================================================== --- (empty file) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java Tue Aug 8 23:13:08 2006 @@ -0,0 +1,148 @@ +package ch.qos.logback.core.net; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import ch.qos.logback.core.AppenderBase; +import ch.qos.logback.core.Layout; + +public abstract class SyslogAppenderBase extends AppenderBase { + + Layout layout; + int facility; + String facilityStr; + String syslogHost; + SyslogWriter sw; + //private String localHostname; + + public void start() { + int errorCount = 0; + if (facilityStr == null) { + addError("The Facility option is mandatory"); + errorCount++; + } + + facility = facilityStringToint(facilityStr); + + layout = getLayout(); + + if(errorCount == 0) { + super.start(); + } + } + + abstract public Layout makeDefaultLayout(int facility); + + abstract public int getSeverityForEvent(Object eventObject); + + @Override + protected void append(Object eventObject) { + if(!isStarted()) { + return; + } + try { + String msg = layout.doLayout(eventObject); + sw.write(msg); + sw.flush(); + } catch(IOException ioe) { + addError("Failed to send diagram to "+syslogHost, ioe); + stop(); + + } + } + + /** + * Returns the integer value corresponding to the named syslog facility. + * + * @throws IllegalArgumentException if the facility string is not recognized + * */ + static int facilityStringToint(String facilityStr) { + if ("KERN".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_KERN; + } else if ("USER".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_USER; + } else if ("MAIL".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_MAIL; + } else if ("DAEMON".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_DAEMON; + } else if ("AUTH".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_AUTH; + } else if ("SYSLOG".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_SYSLOG; + } else if ("LPR".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LPR; + } else if ("NEWS".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_NEWS; + } else if ("UUCP".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_UUCP; + } else if ("CRON".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_CRON; + } else if ("AUTHPRIV".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_AUTHPRIV; + } else if ("FTP".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_FTP; + } else if ("LOCAL0".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL0; + } else if ("LOCAL1".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL1; + } else if ("LOCAL2".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL2; + } else if ("LOCAL3".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL3; + } else if ("LOCAL4".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL4; + } else if ("LOCAL5".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL5; + } else if ("LOCAL6".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL6; + } else if ("LOCAL7".equalsIgnoreCase(facilityStr)) { + return SyslogConstants.LOG_LOCAL7; + } else { + throw new IllegalArgumentException(facilityStr + " is not a valid syslog facility string"); + } + } + + + /** + * Returns the value of the <b>SyslogHost</b> option. + */ + public String getSyslogHost() { + return syslogHost; + } + + /** + * The <b>SyslogHost</b> option is the name of the the syslog host where log + * output should go. + * + * <b>WARNING</b> If the SyslogHost is not set, then this appender will fail. + */ + public void setSyslogHost(String syslogHost) { + this.syslogHost = syslogHost; + } + + /** + * Returns the string value of the <b>Facility</b> option. + * + * See {@link #setFacility} for the set of allowed values. + */ + public String getFacility() { + return facilityStr; + } + + /** + * The <b>Facility</b> option must be set one of the strings KERN, + * USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP, + * NTP, AUDIT, ALERT, CLOCK, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, + * LOCAL6, LOCAL7. Case is not important. + * + * <p>See {@link SyslogConstants} and RFC 3164 for more information about the + * <b>Facility</b> option. + */ + public void setFacility(String facilityStr) { + if(facilityStr != null) { + facilityStr = facilityStr.trim(); + } + this.facilityStr = facilityStr; + } +} Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogConstants.java ============================================================================== --- logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogConstants.java (original) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogConstants.java Tue Aug 8 23:13:08 2006 @@ -1,14 +1,12 @@ package ch.qos.logback.core.net; /** - * Constants used by syslog daemon and transitively by {@link SyslogAppender}. + * Constants used by syslog daemon and transitively by {@link SyslogAppenderBase}. * * @author Ceki Gülcü **/ public class SyslogConstants { - - // Following constants extracted from RFC 3164, we multiply them by 8 // in order to precompute the facility part of PRI. // See RFC 3164, Section 4.1.1 for exact details. Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogWriter.java ============================================================================== --- logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogWriter.java (original) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/net/SyslogWriter.java Tue Aug 8 23:13:08 2006 @@ -12,7 +12,7 @@ * SyslogWriter is a wrapper around the {@link DatagramSocket} class * so that it behaves like a {@link Writer}. */ -public class SyslogWriter extends Writer { +class SyslogWriter extends Writer { /** * The maximum length after which we discard the existing string buffer and * start anew. Modified: logback/core/trunk/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java ============================================================================== --- logback/core/trunk/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java (original) +++ logback/core/trunk/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java Tue Aug 8 23:13:08 2006 @@ -27,6 +27,8 @@ Converter head; String pattern; + Map<String, String> instanceConverterMap = new HashMap<String, String>(); + /** * Concrete implementations of this class are responsible for elaborating the * mapping between pattern words and converters. @@ -39,20 +41,30 @@ * Returns a map where the default converter map is merged with the map * contained in the context. */ - public Map<String, String> getConverterMap() { - Map<String, String> map = new HashMap<String, String>(); + public Map<String, String> getEffectiveConverterMap() { + + Map<String, String> effectiveMap = new HashMap<String, String>(); + + + // add the least specific map fist Map<String, String> defaultMap = getDefaultConverterMap(); if (defaultMap != null) { - map.putAll(defaultMap); + effectiveMap.putAll(defaultMap); } + + // contextMap is more specific than the default map Context context = getContext(); if (context != null) { Map<String, String> contextMap = context.getConverterMap(); if (contextMap != null) { - map.putAll(contextMap); + effectiveMap.putAll(contextMap); } } - return map; + + // set the most specific map last + effectiveMap.putAll(instanceConverterMap); + + return effectiveMap; } public void start() { @@ -62,7 +74,7 @@ p.setStatusManager(getContext().getStatusManager()); } Node t = p.parse(); - this.head = p.compile(t, getConverterMap()); + this.head = p.compile(t, getEffectiveConverterMap()); postCompileProcessing(head); DynamicConverter.startConverters(this.head); super.start(); @@ -129,4 +141,8 @@ } return c; } + + public Map<String, String> getInstanceConverterMap() { + return instanceConverterMap; + } } Modified: logback/core/trunk/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java ============================================================================== --- logback/core/trunk/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java (original) +++ logback/core/trunk/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java Tue Aug 8 23:13:08 2006 @@ -46,10 +46,12 @@ */ public void testConverterStart() { PatternLayoutBase plb = getPatternLayoutBase(); - plb.getConverterMap().put("EX", ExceptionalConverter.class.getName()); + plb.getInstanceConverterMap().put("EX", ExceptionalConverter.class.getName()); plb.setPattern("%EX"); plb.start(); - plb.doLayout(getEventObject()); + String result = plb.doLayout(getEventObject()); + assertFalse(result.contains("%PARSER_ERROR_EX")); + System.out.println("========="+result); } public void testStarted() {