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

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.

Related

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.

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

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"/>

How can we use variables in wxl file [duplicate]

I need to use variable in WIX localization file WIXUI_en-us.wxl.
I tried use it like this:
<String Id="Message_SomeVersionAlreadyInstalled" Overridable="yes">A another version of product $(var.InstallationVersionForGUI) is already installed</String>
But it doesn't work. And when I declared property and used it this way:
<String Id="Message_SomeVersionAlreadyInstalled" Overridable="yes">A another version of product [InstallationVersionForGUI] is already installed</String>
doesn't work either.
Where was I wrong?
Thanks for help and your time.
Localization strings are processed at link time, so you can't use $(var) preprocessor variables. Using a [property] reference is supported, as long as the place where the localization string is used supports run-time formatting (e.g., using the Formatted field type).
Your second method should work just fine. This is the same method used by the default .wxl files.
For example, in your .wxl file you would declare your string:
<String Id="Message_Foo">Foo blah blah [Property1]</String>
And in your .wxs file, you declare the property. If you wish, you can declare the property to match a WiX variable (which it sounds like you're trying to do)
<Property Id="Property1">$(var.Property1)</Property>
I was trying to get localization file to use variables. Came across this post:
There are different layers of variables in WiX (candle's preprocessor
variables, Light's WixVariables/localization variables/binder
variables, and MSI's properties). Each have different syntax and are
evaluated at different times:
Candle's preprocessor variables "$(var.VariableName)" are evaluated
when candle runs, and can be set from candle's commandline and from
"" statements. Buildtime environment
properties as well as custom variables can also be accessed similarly
(changing the "var." prefix with other values).
Light's variables accessible from the command-line are the
WixVariables, and accessing them is via the "!(wix.VariableName)"
syntax. To access your variable from your commandline, you would need
to change your String to: This build was prepared on
!(wix.BuildMachine)
If you instead need to have the BuildMachine value exist as an MSI
property at installation time (which is the "[VariableName]" syntax)
you would need to add the following to one of your wxs files in a
fragment that is already linked in:
Now, the environment variable COMPUTERNAME always has held the name of
my build machines in the past, and you can access that this way:
$(env.COMPUTERNAME). So, you can get rid of the commandline addition
to light.exe and change your wxs file like this:
<WixProperty Id="BuildMachine" Value="$(env.COMPUTERNAME)"/>
Preprocessor variables $(var.VariableName) are are processed at link time, so ideally you would use [PropertyName] which would be defined on the main Product element.
The issue sometimes is that property is not yet defined, for instance using the product name on the localization file seems not posible.
This solution was done aiming to only type the product name once given "Super product" as product name:
In case of running through visual studio extension:
Project properties -> Build -> Define variables -> "MyProductName=Super product" (No quotes)
In case of runing from cmd or some other place:
On Light.exe, add -d"MyProductName=Super product"
Into the localization .wxl file:
<String Id="Description" Overridable="yes">Description of !(wix.MyProductName)
to make it more interesting</String>
I have an aditional config file .wxi I include on other files to have some vars, for instance, here i had hardcoded the value but now it's harcoded on the variable definition and I use the given value:
<?xml version="1.0" encoding="utf-8"?>
<Include>
<!-- Define the product name preprocesor variable -->
<?define ProductName="!(wix.ProductNameDefVar)" ?>
<!-- From this point, can use the preprocesor var -->
<?define ProductName_x64="$(var.ProductName) (64bit)" ?>
<?define ProductName_x32="$(var.ProductName) (32bit)" ?>
<?define CompanyDirName = "My company name" ?>
</Include>
Finally, the place where the localization value where the localization text was not interpolating, is like this:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<!-- Include the config file with the preprocesor var -->
<?include $(sys.CURRENTDIR)\Config.wxi?>
<!-- Main product definition -->
<Product Id="$(var.ProductCode)"
Name="$(var.ProductName)"
Language="!(loc.Language)"
Version="$(var.BuildVersion)"
Manufacturer="!(loc.Company)"
UpgradeCode="$(var.UpgradeCode)">
<!-- Package details -->
<!-- Here, Description was not interpolating -->
<Package InstallerVersion="200"
Compressed="yes"
InstallScope="perMachine"
Platform="$(var.Platform)"
Manufacturer="!(loc.Company)"
Description="!(loc.Description)"
Keywords="!(loc.Keywords)"
Comments="!(loc.Comments)"
Languages="!(loc.Language)"
/>
[...]

Resources