Author: rgoers Date: Sun May 30 08:42:02 2010 New Revision: 949493 URL: http://svn.apache.org/viewvc?rev=949493&view=rev Log: Comment on @doubt entries
Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/pattern/IETFSyslogStartConverter.java Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/FilterBase.java Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=949493&r1=949492&r2=949493&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java Sun May 30 08:42:02 2010 @@ -74,8 +74,10 @@ public class LoggerContext implements or } /** - * @doubt no check for null, could cause NPE if reconfigure is called. - */ + * @doubt no check for null, could cause NPE if reconfigure is called. (RG) I started to fix + * this and realized the proper fix was to check for null and if null throw a LoggingException. Is + * that really better than an NPE? + */ public synchronized Configuration setConfiguration(Configuration config) { Configuration prev = this.config; this.config = config; Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/FilterBase.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/FilterBase.java?rev=949493&r1=949492&r2=949493&view=diff ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/FilterBase.java (original) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/FilterBase.java Sun May 30 08:42:02 2010 @@ -30,7 +30,8 @@ import org.apache.logging.log4j.message. * an appender. A filter may choose to support being called only from the context or only from an appender in * which case it will only implement the required method(s). The rest will default to return NEUTRAL. * - * @doubt why extend FilterBase instead of implementing Filter. + * @doubt why extend FilterBase instead of implementing Filter. (RG) Because all filters have a match or + * mismatch and some filters may choose to not implement all the filter methods. */ public abstract class FilterBase implements Filter { Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/pattern/IETFSyslogStartConverter.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/pattern/IETFSyslogStartConverter.java?rev=949493&view=auto ============================================================================== --- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/pattern/IETFSyslogStartConverter.java (added) +++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/pattern/IETFSyslogStartConverter.java Sun May 30 08:42:02 2010 @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.layout.pattern; + +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.message.StructuredDataMessage; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Map; + +/** + * + */ +...@plugin(name="IETFSyslogStartPatternConverter", type="Converter") +...@converterkeys({"n","syslogStart"}) +public class IETFSyslogStartConverter extends LogEventPatternConverter { + + private static final DecimalFormat TWO_DIGIT = new DecimalFormat("00"); + private static final DecimalFormat FOUR_DIGIT = new DecimalFormat("0000"); + + long lastTimestamp = -1; + String timesmapStr = null; + SimpleDateFormat simpleFormat; + String localHostName; + int facility; + + String appName; + String messageId; + + public void start() { + int errorCount = 0; + + String facilityStr = getFirstOption(); + if (facilityStr == null) { + addError("was expecting a facility string as an option"); + return; + } + facility = SyslogAppenderBase.facilityStringToint(facilityStr); + + Map<SyslogOption, String> options = ConverterOptions.getOptions(SyslogOption.class, getOptionList()); + + for (Map.Entry<SyslogOption, String> entry : options.entrySet()) { + switch (entry.getKey()) { + case APPNAME: + appName = entry.getValue(); + break; + case MESSAGEID: + messageId = entry.getValue(); + break; + } + } + + localHostName = getLocalHostname(); + try { + simpleFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + } catch (IllegalArgumentException e) { + addError("Could not instantiate SimpleDateFormat", e); + errorCount++; + } + + if(errorCount == 0) { + super.start(); + } + } + + public void format(LogEvent event, final StringBuilder toAppendTo) { + + int pri = facility + LevelToSyslogSeverity.convert(event); + + toAppendTo.append("<"); + toAppendTo.append(pri); + toAppendTo.append(">1 "); + toAppendTo.append(computeTimeStampString(event.getMillis())); + toAppendTo.append(' '); + toAppendTo.append(localHostName); + toAppendTo.append(' '); + if (appName != null) { + toAppendTo.append(appName); + /* } else if (event.getLoggerContextVO().getName() != null) { + toAppendTo.append(event.getLoggerContextVO().getName()); */ + } else { + toAppendTo.append("-"); + } + toAppendTo.append(" "); + toAppendTo.append(getProcId()); + toAppendTo.append(" "); + String type = getStructuredType(event.getMessage()); + if (type != null) { + toAppendTo.append(type); + } else if (messageId != null) { + toAppendTo.append(messageId); + } else { + toAppendTo.append("-"); + } + toAppendTo.append(" "); + return toAppendTo.toString(); + } + + private String getStructuredType(Message msg) { + if (msg == null || !(msg instanceof StructuredDataMessage)) { + return null; + } + return ((StructuredDataMessage) msg).getType(); + } + + String getProcId() { + return "-"; + } + + /** + * 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"; + } + } + + String computeTimeStampString(long now) { + synchronized (this) { + if (now != lastTimestamp) { + lastTimestamp = now; + StringBuilder buf = new StringBuilder(); + Calendar cal = new GregorianCalendar(); + cal.setTimeInMillis(now); + buf.append(FOUR_DIGIT.format(cal.get(Calendar.YEAR))); + buf.append("-"); + buf.append(TWO_DIGIT.format(cal.get(Calendar.MONTH) + 1)); + buf.append("-"); + buf.append(TWO_DIGIT.format(cal.get(Calendar.DAY_OF_MONTH))); + buf.append("T"); + buf.append(TWO_DIGIT.format(cal.get(Calendar.HOUR_OF_DAY))); + buf.append(":"); + buf.append(TWO_DIGIT.format(cal.get(Calendar.MINUTE))); + buf.append(":"); + buf.append(TWO_DIGIT.format(cal.get(Calendar.SECOND))); + + int millis = cal.get(Calendar.MILLISECOND); + if (millis != 0) { + buf.append(".").append((int) ((float) millis / 10F)); + } + + int tzmin = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000; + if (tzmin == 0) { + buf.append("Z"); + } else { + if (tzmin < 0) { + tzmin = -tzmin; + buf.append("-"); + } else { + buf.append("+"); + } + int tzhour = tzmin / 60; + tzmin -= tzhour * 60; + buf.append(TWO_DIGIT.format(tzhour)); + buf.append(":"); + buf.append(TWO_DIGIT.format(tzmin)); + } + timesmapStr = buf.toString(); + } + return timesmapStr; + } + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-dev-h...@logging.apache.org