Log4j: Log only message string without logging className and other details - log4j2

I have a java application and I am using log4j2.
I want to only log the log message without logging things like className, log level, timestamp etc.
I do see some suggestions regarding custom appender etc. But isn't there a simpler way to achieve this by just modifying the default configuration ?
Example:
log.info(" Hi This is a log message");
Desired Log statement
Hi This is a log message

You have to change config file log4j.xml
The element need to be change in that file is pattern :
if you are using log4j.properties then change corresponding element.
example of pattern element:
<PatternLayout pattern="%d [%p] %c %m%n"/>
here d id for date
p is for priority
c is for Class Name
m is fro message
n is fro separator
if you dont want any thing then remove from pattern entry.
Modified as per your requirement
<PatternLayout pattern=" %m%n"/>

Related

How to pass value in log4j2 JSON layout configuration the value of KeyValuePair from code?

I want to pass some value from my code to my logs which in JSON format. The key of that value should define in log configuration file.
My XML configuration log4j2-spring.xml is as follow:
<Console name="Console" target="SYSTEM_OUT">
<JsonLayout complete="false" stacktraceAsString="true" eventEOL="true" compact="true">
<KeyValuePair key="ValueFromCode" value="Value"/>
</JsonLayout>
</Console>
When I write
Logger logger = LogManager.getLogger(this.getClass());
logger.info("TestXXX")
the above configuration writes log as
{"instant":{"epochSecond":1625138085,"nanoOfSecond":35827700},"thread":"XXXXX","level":"INFO","loggerName":"XXXXXXX","message":"TestXXX","endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","threadId":XX,"threadPriority":X,"ValueFromCode":"Value"}
I want to pass the value of this Key ValueFromCode from my code. Something like,
Logger logger = LogManager.getLogger(this.getClass());
logger.info("TestXXX","some value for Key ValueFromCode")
and this should write my log as
{"instant":{"epochSecond":1625138085,"nanoOfSecond":35827700},"thread":"XXXXX","level":"INFO","loggerName":"XXXXXXXXXX","message":"TestXXX","endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","threadId":XX,"threadPriority":X,"ValueFromCode":"some value for Key ValueFromCode"}
Please tell me if this is achievable. If there is any alternate way to achieve the same.

XSLT 2.0: Check if string within a node-set is contained in another string

I have a requirement in which the input XML that is received has different error description for the same error code. I need to compare whether a part of the text is contained within the error description in order to do some filtering. Below is the snippet of what I am trying to do.
Created a variable to store a list of all the partial text to be checked within the error description.
<xsl:variable name="partialTextList">
<errorDesc value="insufficient funds" />
<errorDesc value="amount cannot exceed" />
</xsl:variable>
Created a key to access the variable
<xsl:key name="kErrorDesc" match="errorDesc" use="#value" />
The input XML to this XSL will have something like
<Error>
<Code>123</Code>
<Desc>Transaction cannot be processed as account has insufficient funds.</Desc>
</Error>
OR
<Error>
<Code>123</Code>
<Desc>The withdrawal amount cannot exceed account balance.</Desc>
</Error>
Is it possible to use contains function to check whether <Desc> has one of the values from partialTextList?
I tried to look up a solution for this comparison but was not able to find one. Most of the solutions are to check whether <Desc> value is present in the list but not vice-versa.
Any help is appreciated.
In the context of e.g. xsl:template match="Error" you can certainly check $partialTextList/errorDesc/#value[contains(current()/Desc, .)] or move it to the pattern xsl:template match="Error[$partialTextList/errorDesc/#value[contains(current()/Desc, .)]]" if you like.

log4j2,yml - ThreadContext value is empty

I am using spring-boot-starter-log4j2:2.1.2.RELEASE with log4j2.yml file.
on my application code,
private static final Logger logger = LogManager.getLogger(LoggingAspect.class);
...
ThreadContext.put("hostname", InetAddress.getLocalHost().getHostName());
...
logger.error("message");
and log4j2.yml file,
Appenders:
RollingRandomAccessFile:
- name: Alerts
filename: ...
filePattern: ...
PatternLayout:
Pattern: "%d{yyyy-MM-dd HH:mm:ss} %-5p %X{hostname}:%m%n"
on log file, hostname is missing while timestamp and message is showing as expected.
I've tried ${ctx:hostname}, $${ctx:hostname} but so far nothing is working for me.
Please, guide me.
Thanks!
%X{hostname} would only work if you added a hostname value to the ThreadContext. But you don’t need to do that. Log4J automatically populates a hostname variable for you. So just specify ${hostName} in your pattern.

Unable to parse XML with multiple namespaces using tcl and tdom

I am trying to parse a XML using tcl and tdom package. I am having trouble doing this as the node I want to parse is a child to a node with multiple namespaces. How would I be able to parse the realmCode or title element? Below is what I have tried:
package require tdom
set XML {<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.cerner.com/cda_stylesheet/" ?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sdtc="urn:hl7-org:sdtc" xsi:schemaLocation="urn:hl7-org:v3 ../../../CDA%20R2/cda-schemas-and-samples/infrastructure/cda/CDA.xsd" classCode="DOCCLIN" moodCode="EVN">
<realmCode code="US" />
<title>Discharge Summary</title>
</ClinicalDocument>}
set nsmap {
a urn:hl7-org:v3
x http://www.w3.org/2001/XMLSchema-instance
s urn:hl7-org:sdtc
}
set doc [dom parse $XML]
set root [$doc documentElement]
set node [$root selectNodes -namespaces $nsmap "/a:ClinicalDocument/title"]
#set node [$root selectNodes "/ClinicalDocument/title"] ;# tried this as well - does not work
$doc delete
You need to specify the namespace for every level of the path, not just the root. Use
set title [$root selectNodes -namespaces $nsmap /a:ClinicalDocument/a:title]
set realm [$root selectNodes -namespaces $nsmap /a:ClinicalDocument/a:realmCode/#code]
etc.
This is more for the sake of completeness, and not necessarily recommended. You may instruct tDOM to simply ignore the namespaces. See -ignorexmlns.
Watch:
set doc [dom parse -ignorexmlns $XML]
set root [$doc documentElement]
$root selectNodes "/ClinicalDocument/title"
$root selectNodes "/ClinicalDocument/realmCode/#code"
The consequences are obvious: ambiguity.

Log4j2 RollingFile doesn't work when using ${sys:jboss.server.log.dir}

Everything works fine when I have a rolling file configured as so:
RollingFile name="RollingFileAppender"
fileName="/applicationLogs/CTMSApplicationService-${hostName}.log"
filePattern="/applicationLogs/CTMSApplicationService-${hostName}-%d{MM-dd-yyyy}-%i.log"
BUT as soon as I use the ${sys:jboss.server.log.dir} for the "root" of my log location the primary log gets created but nothing ever rolls. The log file just gets appended to:
RollingFile name="RollingFileAppender"
fileName="${sys:jboss.server.log.dir}/CTMSApplicationService-${hostName}.log"
filePattern="${sys:jboss.server.log.dir}/CTMSApplicationService-${hostName}-%d{MM-dd-yyyy}-%i.log">
The value of ${sys:jboss.server.log.dir}="E:\Wildfly-8.1.0.Final\standalone\log"
Is it because the fileName can handle the Winblows "E:\" and the filePattern can not?
This may be caused by this issue: https://issues.apache.org/jira/browse/LOG4J2-829
This is fixed in the master branch and will be included in the upcoming 2.1 release.

Resources