within the log4j2 configuration I am trying to convert the loglevels from string to an int.
Following the directions located in log4j2 Patterns section
image snippet of example log4j2 pattern mapping
Following the example above I created in log4j2.xml config:
<JMS name="jmsQueue"
destinationBindingName="${sys:env}.logging"
factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
factoryBindingName="ConnectionFactory"
providerURL="${sys:log4j.providerurl}"
userName="log"
password="log">
<PatternLayout pattern='{"Message":"%m","LogLevel":%level{Debug=1,Info=2,Warn=3,Error=4,Fatal=5,Trace=6},"Type":"middleware","App":"${app_name}","Env":"${sys:env}","data":{"Event_Time":"%d{ISO8601}","Thread":"%t","Class":"%c"}}'/>
</JMS>
The edited PatternLayout I would now expect would replace the string debug,info,warn etc. with the mapped int values of 1,2,3 . .
This is not the case, I still receive string value. If someone could suggest where I am making the mistake I would appreciate it. thank you.
edit:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<!-- replace > app_name_here_only < with the distinct name of your service. you will use this name to search for within kibana (ex: type:middleware app:<app_name>) -->
<Properties>
<Property name="app_name">middleware_united</Property>
</Properties>
<!-- do not configure below this line -->
<Appenders>
<RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${app_name}.log" filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${app_name}-%i.log">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<JMS name="jmsQueue" destinationBindingName="${sys:env}.logging" factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory" factoryBindingName="ConnectionFactory" providerURL="${sys:log4j.providerurl}" userName="log" password="log">
<PatternLayout pattern='{"Type":"middleware","Env":"${sys:env}","App":"${app_name}","LogLevel":%level{Debug=1,Info=2,Warn=3,Error=4,Fatal=5,Trace=6},"Message":"%m","data":{"Event_Time":"%d{ISO8601}","Thread":"%t","Class":"%c"}}'/>
</JMS>
</Appenders>
<Loggers>
<!-- CXF is used heavily by Mule for web services -->
<AsyncLogger name="org.apache.cxf" level="WARN"/>
<!-- Apache Commons tend to make a lot of noise which can clutter the log-->
<AsyncLogger name="org.apache" level="WARN"/>
<!-- Reduce startup noise -->
<AsyncLogger name="org.springframework.beans.factory" level="WARN"/>
<!-- Mule classes -->
<AsyncLogger name="org.mule" level="INFO"/>
<AsyncLogger name="com.mulesoft" level="INFO"/>
<!-- Reduce DM verbosity -->
<AsyncLogger name="org.jetel" level="WARN"/>
<AsyncLogger name="Tracking" level="WARN"/>
<AsyncRoot level="INFO">
<AppenderRef ref="file"/>
<AppenderRef ref="jmsQueue"/>
</AsyncRoot>
</Loggers>
</Configuration>
I've tested your pattern but it worked well.
Here's my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern='{"Type":"middleware","Env":"${sys:env}","App":"${app_name}","LogLevel":%level{Debug=1,Info=2,Warn=3,Error=4,Fatal=5,Trace=6},"Message":"%m","data":{"Event_Time":"%d{ISO8601}","Thread":"%t","Class":"%c"}}'/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
And here's the output:
{"Type":"middleware","Env":"${sys:env}","App":"${app_name}","LogLevel":2,"Message":"b","data":{"Event_Time":"2017-03-29T11:28:14,477","Thread":"main","Class":"test.Log4j2"}}
Related
I tried integrating my SpringBoot Application with Splunk, but could not get the logs. There is no Error in Console. Kindly help
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{10}}{bright,yellow}: %msg%n%throwable" />
</Console>
<SplunkHttp
name="student-service-token"
url="https://prd-p-wyalk.splunkcloud.com:8080"
token="*********************************"
host="localhost"
index="student_idex_api"
type="raw"
source="http-event-logs"
sourcetype="log4j"
messageFormat="text"
disableCertificateValidation="true">
<PatternLayout pattern="%m" />
</SplunkHttp>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="console" />
<AppenderRef ref="splunkhttp" />
</Root>
</Loggers>
</Configuration>
I want to do graylog integration in java by using "gelf" library. I added config parameters in order to connect graylog server into "log4j_Appender.xml"
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" packages="org.graylog2.log4j2">
<Appenders>
<GELF name="gelfAppender" server="graylog.llm.com" port="12212" hostname="llmtest.llm.com" protocol="UDP" includeThreadContext="true">
<KeyValuePair key="grayloggroup" value="LLMFIGHTTEST" />
<KeyValuePair key="application" value="baggagebustest" />
<KeyValuePair key="environment" value="baggagebustest" />
<!-- <Filters> -->
<!-- <Filter type="MakerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"></Filter> -->
<!-- <Filter type="MakerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"></Filter> -->
<!-- </Filters> -->
<PatternLayout pattern="%m%n"/>
</GELF>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="gelfAppender" />
</Root>
</Loggers>
</Configuration>
But I can not connect to "graylog.llm.com" and I don't add a log into "LLMFIGHTTEST" stream.
I am using this project in github.
We should use add appender into log4j.xml file and add dependency log4j2-gelf as maven dependency.
Dependency,
<dependency> <groupId>org.graylog2.log4j2</groupId> <artifactId>log4j2-gelf</artifactId> <version>1.3.2-SNAPSHOT</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" packages="org.graylog2.log4j2">
<Appenders>
<GELF name="gelfAppender" server="graylog.test.com" port="12212" hostname="test.th.com" protocol="UDP" includeThreadContext="true">
<KeyValuePair key="ORDERID" value="$${ctx:ORDERID}"/>
<Filters>
<Filter type="MakerFilter" marker="PARENT" onMatch="DENY" onMismatch="NEUTRAL"></Filter>
<Filter type="MakerFilter" marker="TEST" onMatch="DENY" onMismatch="ACCEPT"></Filter>
</Filters>
<PatternLayout pattern="ORDERID : $${ctx:ORDERID} %msg%n" />
</GELF>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%c{1.}] - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="gelfAppender" />
<AppenderRef ref="STDOUT" />
<AppenderRef ref="Error-Appender" level="error"/>
</Root>
</Loggers>
</Configuration>
I have a maven web project which requires logging.
I decided to use log4j2 for that purpose and added the required entries in the pom.xml file
Then I created the log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RollingFile" fileName="${logs.path}/text.log"
filePattern="${logs.path}/%d{YYYYMMdd}-text.%i.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="info" additivity="false">
<appender-ref ref="RollingFile" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I'm starting tomcat with -Dlogs.path="C:\mylogs", where C:\mylogs exists and has public read/write access.
The console logger is working fine, I can see the correct output in my console, the issue is that the file isn't being created, so I have no logfile, everything gets logged to catalina.out only, and the tomcat startup logs don't show errors for log4j.
What am I missing? is there some additional configuration I need to do in order to log to file?
I went through the documentation. You must refer to system properties with sys:, and it seems that tomcat properties are seen as system properties, so I replaced ${logs.path} with ${sys:logs.path} and it worked.
To create a log file with the log4j2 config you first need to define your appender. Here's an example:
<RollingFile name="MGMT"
fileName="${logdir}/mgmt.log"
filePattern="${logdir}/mgmt.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="mgmt.*.log" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
Afterwards you just need to define a logger that will use it:
<Logger name="the package or name that is displayed in the log line"
level="ALL"
additivity="false">
<AppenderRef ref="MGMT"/>
</Logger>
If you have a log that has com.company.test as package set that package as name for the logger. It's important that they match.
The field additivity will define if you want to pass the catched log to its parent ( true ) or just let this logger handle it ( false )
EDIT:
your config file might need this:
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c- %m%n</Property>
</Properties>
<Appenders>
If you don't want to use this you have to define the path static like <RollingFile name="MGMT"
fileName="C:\path\file.log"
Background: I have log4j.xml file configured for our spring based application, which looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="FATAL" shutdownHook="disable" packages="com.gemstone.gemfire.internal.logging.log4j">
<Properties>
<Property name="gemfire-pattern">[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${gemfire-pattern}"/>
</Console>
<RollingFile name="eventLogFile" fileName="/opt/data/service/logs/events.log"
filePattern="/opt/data/service/logs/events-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS} %p - %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="20" fileIndex="max"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.gemstone" level="INFO" additivity="true">
<filters>
<MarkerFilter marker="GEMFIRE_VERBOSE" onMatch="DENY" onMismatch="NEUTRAL"/>
</filters>
</Logger>
<Logger name="com.app.mypackage" level="INFO" additivity="true">
<AppenderRef ref="eventLogFile"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
Now, I want log4j to write log statements with let's say 'countryName'. And, this 'countryName' should be configured via external property file.
For e.g, the "gemfire-pattern" will have this externalised property name $${countryName}.
<Property name="gemfire-pattern">[$${countryName} %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
Considering this log4j system properties, in my case, the log4j.component.properties is not being picked up by log4j.
Any thoughts on how to fetch a property value from external properties file in log4j.xml?
References:
how-to-read-property-variable-from-file-into-log4j2
log4j system properties
log4j properties substitution
Thanks in advance.
log4j.component.properties is used to add log4j specific system properties like log4j.configurationFile, org.apache.logging.log4j.level etc.
To refer to user defined properties, include the property file inside the logback configuration and refer to the keys using ${KEY}
<configuration>
<property file="country.properties" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${countryName}</file>
...
Logback also allows you to externalise parts of the configuration using File inclusion feature.
https://logback.qos.ch/manual/configuration.html#fileInclusion
<configuration>
<include file="src/main/java/chapters/configuration/includedConfig.xml"/>
...
Make sure the content in the external xml file is encolsed with <included> </included> tag
Note- System properties(-Dcountry="") and Environment variables can also be referred using ${PROPERTY_NAME} inside the logback configuration.
I've implemented async logging with log4j 2, but now I need to change log filename every hour, eg 2015-11-19/log-12.00.log, 2015-11-19/log-13.00, etc. (Soulutions I've found didn't work, may be I did something wrong).
I have following log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="info" includeLocation="false">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
How to achive this?
You should have a look at TimeBasedTriggeringPolicy. Basically it causes a rollover once the date/time pattern no longer applies to the active file.
Have't tried this but this should work for you.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" >
<appenders>
<Async name="Async">
<AppenderRef ref="logfile" />
</Async>
<RollingRandomAccessFile name="logfile" fileName="async.log" filePattern="log-%d{HH}.00.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="24"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<root level="INFO" includeLocation="false">
<AppenderRef ref="Async"/>
</root>
</loggers>
</configuration>
The TimeBasedTriggeringPolicy interval attribute controls how often a rollover should occur based on the most specific time unit in the date pattern. In you case it is hours that is %d{HH} from file pattern.