We have a log4j2-graylog.xml file that we use to bridge our log4j output to GrayLog 3.0 using the biz.paluch.logging.gelf.log4j2 package. This config file is based on this example:
<?xml version="1.0" encoding="utf-8"?>
<Configuration monitorInterval="60" packages="biz.paluch.logging.gelf.log4j2">
<Appenders>
<Gelf name="gelf"
host="10.13.10.192"
port="12201"
version="1.1"
extractStackTrace="true"
filterStackTrace="true"
mdcProfiling="true"
includeFullMdc="true"
maximumMessageSize="8192"
originHost="swarm"
additionalFieldTypes="environment=String,application=String">
<Field name="timestamp" pattern="%d{dd MMM yyyy HH:mm:ss,SSS}"/>
<Field name="level" pattern="%level"/>
<Field name="simpleClassName" pattern="%C{1}"/>
<Field name="className" pattern="%C"/>
<Field name="server.fqdn" pattern="%host{fqdn}"/>
<Field name="threadName"/><!-- didn't seem to work -->
<!-- This is a static field -->
<Field name="application" literal="PISM-webapp"/>
</Gelf>
</Appenders>
<Loggers>
<Logger name="com.xxx" level="DEBUG" additivity="false">
<AppenderRef ref="gelf"/>
</Logger>
</Loggers>
</Configuration>
It basically works. Our Java calls to log4j logging show up in GrayLog.
Based on the information from here, it seems like it ought to be easy to add a field that captures the thread data, but our attempts thus far have failed. Also we would like a field that reports the value of a certain environment variable. Can anybody tell me how to do either of these two things?
Based on information from the log4j2 manual, I was able to get the thread name by adding threadName to the additionalFieldTypes and using a %t pattern specifier like so:
<Gelf name="gelf"
host="10.13.10.192"
port="12201"
version="1.1"
extractStackTrace="true"
filterStackTrace="true"
mdcProfiling="true"
includeFullMdc="true"
maximumMessageSize="8192"
originHost="swarm"
additionalFieldTypes="environment=String,application=String,threadName=String,fbHost=String">
...
<Field name="threadName" pattern="%t"/>
However, when I tried to use the syntax provided on the page to retrieve the value of an environment variable like so:
<Field name="fbHost" pattern="$${env:LB_COOKIE_VALUE}" />
I didn't get the value of the LB_COOKIE_VALUE environment value in GrayLog. Instead, I got the literal string - "${env:LB_COOKIE_VALUE}". This StackOverflow post gave enough information to debug the problem. When I provided a default value,
<Field name="fbHost" pattern="$${env:LB_COOKIE_VALUE:-unset_env_LB_COOKIE_VALUE}" />
I got "unset_env_LB_COOKIE_VALUE" in the output, indicating that the environment variable was not set. However, the Java system property was set, so
<Field name="fbHost" pattern="$${sys:LB_COOKIE_VALUE:-unset_sys_LB_COOKIE_VALUE}" />
provided the value I was looking for.
Related
Is there a way that I can exclude a custom Exception class from the logging system? I know I can filter out by Category but I would like just to keep one self-made exception class out.
I don't see a way with the built-in filters. But you could use NLog with the ASP.NET Core logging system. You're writing the log messages the same way as before, but it provides you a wider range of configuration, including a way to filter specific exception types.
Sample
Let's create a nlog.config with two target log files: one with all messages, including the unwanted exception type and one without this type:
<targets>
<target xsi:type="File"
name="all"
fileName="all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<target xsi:type="File"
name="filtered"
fileName="filtered-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
</targets>
The exclusion of the unwanted exception types is configured within the logging rules. In this example, we're filtering the exception type SampleApplication.Exceptions.SampleException:
<rules>
<logger name="*" minlevel="Trace" writeTo="all" />
<logger name="*" minlevel="Trace" writeTo="filtered">
<filters>
<when condition="contains('${exception:format=Type}', 'SampleApplication.Exceptions.SampleException')"
action="Ignore" />
</filters>
</logger>
</rules>
When I use log4j, I can create a new class extends org.apache.log4j.HTMLLayout and override it to make a new format of the log. When using log4j2, I can only use the layout but not override it. In my log4j2.xml writing
<Appenders>
<File name="log" fileName="D:\log.html" append="false">
<HTMLLayout/>
</File>
</Appenders>
in log4j I may use layout class="log.FormatHTMLLayout" (log.FormatHTMLLayout is my new class which extends the HTMLLayout), but now I can only use HTMLLayout.
Is there any way to override the HTMLayout? I need to do a lot of things, like changing the output table, the title and so on.
Here is what i did,
I needed to add <img> HTML tags in logs generated by log4j2 and by default HTML elements like <, >, " are replaced by escape characters like lt; , gt; , quot;.
(Ref:https://issues.apache.org/jira/browse/LOG4J2-439)
So i just copied whole HTMLLayout class from source, which is available at
log4j-core / src / main / java / org / apache / logging / log4j / core / layout
and changed its name to "CustomHTMLLayout" and updated it wherever required (you can choose any name), now your custom layout class is as good as HTMLLayout class.
there is method called toSerializable which contains actual formatting of each record, so you can manipulate it as per your need.
once class is modified, you need to provide the new layout in your log4j2.html
as following:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.redknee.bssauto.helpers">
<Appenders>
<RollingFile name="Rolling-default" fileName="logs/bssauto.html" filePattern="logs/$${date:yyyy-MM}/bssauto-%d{MM-dd-yyyy}-%i.log.gz">
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Rolling-default"/>
</Root>
</Loggers>
</Configuration>
Notice following
<Configuration packages="com.bssauto.helpers">
here packages should have all packages containing custom class for layouts. so here com.bssauto.helpers is package under which i have CustomHTMLLayout class.
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
and CustomHTMLLayout is custom layout class created by extending AbstractStringLayout
Make sure you are using latest log4j2 version, I used log4j 2.2
Can someone tell me, How to configure custom layout for FileAppender?
Can anyone tell me, how to configure custom layout for FileAppender?
I'm created copy of HTMLLayout and made some changes there (it's cannot be extends because it's final class) and now I want use this layout, but I don't know how :(
This error is showed with bellow listed configuration:
ERROR File contains an invalid element or attribute "ibtrader.log4j2.MYHTMLLayout"
Here is my log4j2.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<configuration strict="true" monitorInterval="30">
<appenders>
<appender name="Console" type="Console" target="SYSTEM_OUT">
<layout type="PatternLayout" pattern="%highlight{%d{ISO8601} [%t] %-5level %logger{36} - %msg%n}" />
</appender>
<appender name="DEBUG_FILE" type="File" fileName="logs/errors.txt" >
<layout type="PatternLayout" pattern="%d{ISO8601} [%t] %-5level %logger{36} - %msg%n}" />
</appender>
<appender name="HTMLAppender" type="File" fileName="logs/mainlog.html">
<layout type="ibtrader.log4j2.MYHTMLLayout" charset="UTF-8" title="IBTRader logs" locationInfo="true" />
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
<appender-ref ref="DEBUG_FILE" level="WARN" />
<appender-ref ref="HTMLAppender" />
</root>
</loggers>
</configuration>
Thanks for help!
The comments mention this is already fixed but just for completeness, a (simplified) configuration with a custom MYHTMLLayout plugin could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- the packages attribute contains a comma-separated list
of the packages that Log4J2 will scan for custom plugins. -->
<configuration packages="ibtrader.log4j2">
<appenders>
<appender name="HTMLAppender" type="File" fileName="logs/mainlog.html">
<!-- Each plugin has a *name*, declared with annotation on the
plugin implementation class.
The plugin name does not need to match the class name.
The plugin name needs to match the *type* attribute in the config.
For example:
package ibtrader.log4j2;
import ...;
#Plugin(name="MYHTMLLayout", category="Core",
elementType="layout", printObject=true)
public class MyHTMLLayoutImpl extends AbstractStringLayout {...
-->
<layout type="MYHTMLLayout" charset="UTF-8" title="IBTRader logs" locationInfo="true" />
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="HTMLAppender" />
</root>
</loggers>
</configuration>
You have to extend AbstractStringLayout, setting the #Plugin properties with category = "Core", elementType = "layout" and name="The name of the class itself that should be refered from the configuration file" (i.e.: "ExtHtmlLayout")
You could just copy the whole class HtmlLayout from the sources and change whatever you want, for example, the time in millis column to the time in hours.
Define the package property in "Configuration" tag to use the package to the extended class you have created.
Finally, just call the new layout from the configuration file:
<ExtHtmlLayout/>
I'm using log4j2-beta9 and want to configure it using a log4j2.xml in strict mode.
My issue is: how do I specify attributes that are not in the shipped schema file?
An Example:
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration
status="DEBUG"
strict="true"
monitorInterval="5"
name="TestingAttributes"
verbose="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Log4j-config.xsd">
<Properties>
</Properties>
<Appenders>
<Appender
type="Console"
name="SYSERR"
target="SYSTEM_ERR"> <!-- cvc-complex-type.3.2.2: Attribute 'target' is not allowed to appear in element 'Appender'. -->
<Layout Type="PatternLayout">
<Pattern>%date{dd.MM.yyyy HH:mm:ss,SSS} %5p %logger %m%n</Pattern>
</Layout>
<Filters>
<Filter
type="MarkerFilter"
marker="FLOW"
onMatch="DENY"
onMismatch="NEUTRAL" />
<Filter
type="MarkerFilter"
marker="EXCEPTION"
onMatch="DENY"
onMismatch="NEUTRAL" />
</Filters>
</Appender>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="SYSERR" />
</Root>
</Loggers>
</Configuration>
Notice that I want to set the appender to have the target SYSTEM_ERR but the attribute is not allowed in strict mode.
target="SYSTEM_ERR"> <!-- cvc-complex-type.3.2.2: Attribute 'target' is not allowed to appear in element 'Appender'. -->
I could always edit the Log4j-config.xsd and allow that attribute there but that would be kind of wrong also because not all appenders have a target attribute.
As searching the web didn't help me so far, I'm asking you:
Is there anything I'm missing in configuring Log4j2 in strict XML mode?
Do I have to "patch" the XMLConfiguration and the schema file and commit a change to log4j or is there another way besides not using strict mode?
Thanks in advance.
Can you ask this on the log4j-user mailing list? It could be a bug in the schema, but I suspect the schema can do with more improvements and your feedback would be valuable.
I'm doing a site definition for SharePoint 2007. When the site is created, a document library called "Folder2" is created also. Now, I need to add some documents to this document library and appear as items in the document library standard views.
My code is:
<Lists>
<List FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101" Type="101" Title="Folder2" Url="Folder2">
<Data>
<Rows>
<Row>
<Field Name="Name">MyFile.txt</Field>
<Field Name="Title">MyFile.txt</Field>
<Field Name="FileLeafRef">MyFile.txt</Field>
</Row>
</Rows>
</Data>
</List>
</Lists>
When I see the items of the Document Library there is one element with title "1_". Does anybody know how to add files in a site definition?
The onet.xml I used is the same as blank site.
Thanks!!!
For Document Libraries, instead of Data/Rows/Row, use Modules:
<Lists>
<List FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101" Type="101" Title="Folder2" Url="Folder2" />
</Lists>
<Modules>
<Module Name="Documents" />
</Modules>
Then in Modules at the bottom of onet.xml, you can define your Module as follows:
<Module Name="Documents" Url="Folder2" Path="">
<File Url="MyFile.txt" Name="MyFile.txt" Type="GhostableInLibrary">
<Property Name="Title" Value="MyFile.txt" />
</File>
</Module>