Custom list properties for custom logback appender - logback

I would like to create a custom LogBack Appender for InfluxDB.
For that, I want to define many series and use the power of logback
<appender name="INFLUXDB" class="org.labaix.logback.InfluxDbAppender">
<series>
<serie>
<id>SensorTemperatureMonthly</id>
<name>machine.%X{machine}.type.%X{type} temperature_${byMonth}</name>
<timeUnit>MILLISECONDS</timeUnit>
</serie>
<serie>
<id>SensorTemperatureDaily</id>
<name>machine.%X{machine}.type.%X{type} temperature_${day}</name>
<timeUnit>MILLISECONDS</timeUnit>
</serie>
</series>
</appender>
Therefore I don't know how to map list of series.
Any ideas ? It is not in documentation so I wonder if it possible.
Thanks. Best regards
Nicolas

Rather than this approach I would prefer to use xml and use JAXB object to load data. You can pass the configuration file xml path as an parameter
<appender name="INFLUXDB" class="org.labaix.logback.InfluxDbAppender">
<configFilePath>configFilePath.xml</configFilePath>
</appender>
The configuration file
<root>
<series>
<serie>
<id>SensorTemperatureMonthly</id>
<name>machine.%X{machine}.type.%X{type} temperature_${byMonth}</name>
<timeUnit>MILLISECONDS</timeUnit>
</serie>
<serie>
<id>SensorTemperatureDaily</id>
<name>machine.%X{machine}.type.%X{type} temperature_${day}</name>
<timeUnit>MILLISECONDS</timeUnit>
</serie>
</series>
</root>

I hit the same problem. I found the answer in ch.qos.logback.core.joran.util.beans.BeanDescription
/**
* Lightweight pendant to the java.beans.BeanInfo class. An instance of this
* class encapsulates the properties of a certain class. The properties are the
* public setters and getters. In addition the 'add-er'-methods are included,
* which are the public methods which start with the prefix 'add'.
*
* #author urechm
*
*/
In this case you could use an addSerie method that adds to a mutable list

Related

Wildfly json-formatter class name metadata

Is it possible to configure a non-static value for the metadata field in the wildly json-formatter?
I didn't find anything about it in the wildfly documentation- it only has a simple static field example (meta-data=[#version=1])
For example, I would like to have a field "simpleClassName" - The class of the code calling the log method.
I also tried to use a similar syntax to pattern-formatter(example below) but it doesn't work
<formatter name="JSON">
<json-formatter>
<meta-data>
<property name="simpleClassName" value="%c{1}"/>
</meta-data>
</json-formatter>
</formatter>
No the meta-data is only static information. However what you're looking for seems to be the details of the caller. Note that this is an expensive operation should you should use it with caution. What you'd want to do is change the print-details to true. In CLI it would be something like:
/subsystem=logging/json-formatter=JSON:write-attribute(name=print-details, value=true)

Is there a checkstyle rule for forcing every field in a class to have an annotation?

We want to make compliance easy and for FedRAMP want something like this on all fields in our database objects
#FedRamp(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
We want checkstyle to break the builds if people add data and forget to add these on 'any' field in the *Dbo.java class. Then, we can generate the FedRAMP compliance on each data item (and therefore the entire system). We run checkstyle on every class but only want this rule run on classes ending in *Dbo.java. Is this possible where we import some already existing checkstyle rule or plugin and add the class name filter to it?
thanks,
Dean
To report violations for such cases for any classes, you can use MatchXpathCheck (you need checkstyle 8.39+)
Config will look like:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name = "Checker">
<module name="TreeWalker">
<module name="MatchXpath">
<property name="id" value="fedramp_check"/>
<property name="query" value="//CLASS_DEF/OBJBLOCK/VARIABLE_DEF/MODIFIERS/ANNOTATION/IDENT[not(#text='FedRamp')]"/>
<message key="matchxpath.match"
value="Field should have 'FedRamp' annotation."/>
</module>
</module>
</module>
This will report violations like this:
$ cat Test.java
class Test {
#FedRamp(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
private int withAnnotation = 11; // no violation
#Fed(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
private int without = 11; // violation
#NotNull
int without = 11; // violation
}
$ java -jar checkstyle-8.42-all.jar -c config.xml Test.java
Starting audit...
[ERROR] C:\workdir\Test.java:6:4: Field should have FedRamp annotation. [fedramp_check]
[ERROR] C:\workdir\Test.java:9:4: Field should have FedRamp annotation. [fedramp_check]
Audit done.
Checkstyle ends with 2 errors.
Second part of your question - narrow execution only to specific classes - can be solved in several ways.
Use a bit different xpath to filter class names (not files, since there can be many classes in single file)
<property name="query"
value="//CLASS_DEF[./IDENT[ends-with(#text,
'Dbo')]]/OBJBLOCK/VARIABLE_DEF/MODIFIERS/ANNOTATION/IDENT[not(#text='FedRamp')]"/>
Use BeforeExecutionExclusionFileFilter - it is filter for whole config and will work ok only if you have a separate config only for checking annotation thing.
Suppress violations for this check (by id, for example) for other class files, see doc

Number of lines read with Spring Batch ItemReader

I am using SpringBatch to write a csv-file to the database. This works just fine.
I am using a FlatFileItemReader and a custom ItemWriter. I am using no processor.
The import takes quite some time and on the UI you don't see any progress. I implemented a progress bar and got some global properties where i can store some information (like lines to read or current import index).
My question is: How can i get the number of lines from the csv?
Here's my xml:
<batch:job id="importPersonsJob" job-repository="jobRepository">
<batch:step id="importPersonStep">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="personItemReader"
writer="personItemWriter"
commit-interval="5"
skip-limit="10">
<batch:skippable-exception-classes>
<batch:include class="java.lang.Throwable"/>
</batch:skippable-exception-classes>
</batch:chunk>
<batch:listeners>
<batch:listener ref="skipListener"/>
<batch:listener ref="chunkListener"/>
</batch:listeners>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="authenticationJobListener"/>
<batch:listener ref="afterJobListener"/>
</batch:listeners>
</batch:job>
I already tried to use the ItemReadListener Interface, but this isn't possible as well.
if you need to know how many lines where read, it's available in spring batch itself,
take a look at the StepExecution
The method getReadCount() should give you the number you are looking for.
You need to add a step execution listener to your step in your xml configuration. To do that (copy/pasted from spring documentation):
<step id="step1">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="10"/>
<listeners>
<listener ref="chunkListener"/>
</listeners>
</tasklet>
</step>
where "chunkListner" is a bean of yours annotated with a method annotated with #AfterStep to tell spring batch to call it after your step.
you should take a look at the spring reference for step configuration
Hope that helps,

Logback - Multiple syslog appenders

I'm logging to syslog with a syslog appender as shown below:
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>localhost</syslogHost>
<facility>LOCAL6</facility>
<suffixPattern>app: %logger{20} %msg</suffixPattern>
</appender>
But I've got a new requirement where I want to send some logs to the "LOCAL5" facility instead of LOCAL6. I've read the logback configuration documentation http://logback.qos.ch/manual/configuration.html but I'm still not sure how to do this.
You can use two syslog appenders one for LOCAL6 and other for LOCAL5
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>localhost</syslogHost>
<facility>LOCAL6</facility>
<suffixPattern>app: %logger{20} %msg</suffixPattern>
</appender>
<appender name="SYSLOG1" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>localhost</syslogHost>
<facility>LOCAL5</facility>
<suffixPattern>app: %logger{20} %msg</suffixPattern>
</appender>
Advantage is that you a customize the logs( some logs) using filters, pattern, logger names etc. And SyslogAppender extends UnsyncronizedAppender.
Reason being you cannot use single appender is that method facilityStringToint(String facilityStr) in class SyslogAppender.java accepts String which then compares which standard facility using equals.
Example
if ("KERN".equalsIgnoreCase(facilityStr)) {
return SyslogConstants.LOG_KERN;
}

How to get JAXB output to have namespace included with the child node with no prefix?

God knows I searched the forum for an answer, but didn't see any.
This is the simplified XML my JAXB code reads. There are 2 namespaces involved. xyz and abc. These two are defined in two different schema files. And xjc generates two different packages for them. The following file is nicely read into those classes and can even write it.
<xyz:xyz xsi:schemaLocation="urn:xyz xyz.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xyz="urn:xyz">
<session>
<App xsi:schemaLocation="urn:abc abc.xsd" xmlns="urn:abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AppItem att1="1234"/>
</App>
</session>
</xyz:xyz>
This is how it writes it.
<ns3:xyz xmlns:ns2="urn:abc" xmlns:ns3="urn:xyz">
<session>
<ns2:App>
<ns2:AppItem att1="1234"/>
</ns2:App>
</session>
</ns3:xyz>
Now i know about NamespacePrefixMapper and I can change ns2 and ns3 to the values I want. And I want this. Basically I want to main the original form of the XML. The App element should have all its information contained in itself and not create a prefix.
<xyz:xyz xmlns:xyz="urn:xyz">
<session>
<App xsi:schemaLocation="urn:abc abc.xsd" xmlns="urn:abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AppItem att1="1234"/>
</App>
</session>
</xyz:xyz>
Does anyone have any clue as to how to achieve this? Seems like some setting in AppType.java should tell the writer to not update root element with prefix.