logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Deboy <scott.de...@gmail.com>
Subject Re: "host" and "application" event properties in LogFileXMLReceiver
Date Fri, 26 Oct 2012 16:42:46 GMT
Hi Jim,

Thanks for the patch - it looks good - I'll apply it.

Scott

On Fri, Oct 26, 2012 at 6:10 AM, Mayer, Jim <Jim.Mayer@xerox.com> wrote:

> Hi,
>
> I've been using a recent trunk version of chainsaw to look at log files
> created with log4j's org.apache.log4j.xml.XMLLayout layout.  I noticed that
> when I configure a LogFileXMLReceiver to open a log file from a well-known
> location that the imported events do not contain a 'hostname' or
> 'application' property.  This, of course, plays havoc with event routing.
>
> A bit of investigation showed that when a log file is opened that the
> FileLoadAction class uses the 'setAdditionalProperties' method on the
> decoder to add the file and path as the host and application properties.
>  This doesn't happen when configuring a LogFileXMLReceiver directly.  When
> I looked at the LogFileXMLReceiver code I saw that it contains the
> following logic (for each event):
>
>                 if (evt.getProperty(Constants.HOSTNAME_KEY) != null) {
>                     evt.setProperty(Constants.HOSTNAME_KEY, host);
>                 }
>                 if (evt.getProperty(Constants.APPLICATION_KEY) != null) {
>                     evt.setProperty(Constants.APPLICATION_KEY, path);
>                 }
>
> This has the effect of setting the hostname and application properties of
> the event to the string "file" and the file path of the configured source
> URL, but only if the event being processed contained "hostname" and
> "application" properties.  This behavior seems somewhat strange... if I've
> gone to the trouble of setting the 'hostname' and 'application' properties
> in the log, I'd expect them to be used.
>
> The following patch changes this behavior as follows:
>
> (1) The event will always have a 'hostname' and 'application' property.
> (2) The default value of the hostname property is 'file'.
> (3) The default value of the application property is the path to the log
> file.
> (4) If an event includes a 'hostname' or 'application' property it will
> override the corresponding default value.
>
> The end result of this change is that files configured with
> LogFileXMLReceiver act, by default, as if they were opened directly (i.e.,
> they use the 'file' URL information for routing).  If the generating
> application adds 'application' or 'hostname' properties to the logged
> events, however, those properties will be used for routing.
>
> I hope you find this useful.
>
> -- Jim Mayer
>
> -------------------- START OF DIFF ------------------
> Index: src/main/java/org/apache/log4j/xml/LogFileXMLReceiver.java
> ===================================================================
> --- src/main/java/org/apache/log4j/xml/LogFileXMLReceiver.java  (revision
> 1397548)
> +++ src/main/java/org/apache/log4j/xml/LogFileXMLReceiver.java  (working
> copy)
> @@ -276,12 +276,17 @@
>          for (Iterator iter = c.iterator(); iter.hasNext();) {
>              LoggingEvent evt = (LoggingEvent) iter.next();
>              if (passesExpression(evt)) {
> -                if (evt.getProperty(Constants.HOSTNAME_KEY) != null) {
> -                    evt.setProperty(Constants.HOSTNAME_KEY, host);
> +               String theHost = host;
> +               String theApp = path;
> +               String v;
> +                if ((v = evt.getProperty(Constants.HOSTNAME_KEY)) !=
> null) {
> +                   theHost = v;
>                  }
> -                if (evt.getProperty(Constants.APPLICATION_KEY) != null) {
> -                    evt.setProperty(Constants.APPLICATION_KEY, path);
> +                if ((v = evt.getProperty(Constants.APPLICATION_KEY)) !=
> null) {
> +                   theApp = v;
>                  }
> +               evt.setProperty(Constants.HOSTNAME_KEY, theHost);
> +               evt.setProperty(Constants.APPLICATION_KEY, theApp);
>                  doPost(evt);
>              }
>          }
> @@ -307,4 +312,4 @@
>          this.useCurrentThread = useCurrentThread;
>      }
>
> -}
> \ No newline at end of file
> +}
> ------------------- END OF DIFF ----------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>
>

Mime
View raw message