SimpleFormatter dd.mm.yyyy HH:mm:ss - java

I want to change the way my log looks (java.util.logging.Logger) and I found this:
System.setProperty("java.util.logging.SimpleFormatter.format","[%1$tF %1$tT] [%4$-7s] %5$s %n");
So my log entries look now like this:
[2017-08-24 15:55:40] [INFORMATION] Hello World!
I'd like to set the format to following:
[24.08.2017 15:55:40]
I've been trying for so long, shouldn't be that hard. Can anybody help me or sending me some good and easy documentations/examples?

From documentation Class Formatter
Instead of :
[%1$tF %1$tT]
You can use :
[%1$te.%1$tm.%1$tY %1$tT]
You can see the result in this example :
Calendar c = Calendar.getInstance();
String example = String.format("[%1$te.%1$tm.%1$tY %1$tT]", c);
System.out.println(example);
Output
[24.08.2017 15:19:30]

I've been trying for so long, shouldn't be that hard.
SimpleFormatter.format is a static property that is set at class loading time. You can only use System.setProperty to change the format if that is done before the java.util.logging.SimpleFormatter class is loaded. Instead you should modify your logging.properties or modify your startup script to set the property during the JVM startup.
If there is an error in your format the behavior is to just use a default format. This makes it hard to debug because you can't tell the difference between an error or just not setting the property correctly.
Your best bet is to use a test program like YCF_L made and then apply the format to the startup script.

Related

Wiremock Date format

I want to generate date in the format 2019-03-28T15:30:59+12:00 using wiremock.
I tried:
"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'T'HH:mm:ssZ'}}"
but, I get exception:
wiremock.com.github.jknack.handlebars.HandlebarsException: inline:
found: ''yyyy-MM-dd'T'HH:mm:ssZ'', expected: 'no viable alternative at
input ''yyyy-MM-dd'T'HH:mm:ssZ'''
I have also tried escaping both the quotes around T, but it does not work.
What I am doing wrong?
Try using following block of code. Working fine for me and probably the best way.
format='yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'
In case anyone else comes across this post in the future, the simplest (but hacky) solution that I used is to format the parts before and after the 'T' separately like so:
"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'}}T{{now timezone='Australia/Sydney' format='HH:mm:ssZ'}}"
A simple workaround:
Declare a variable (myformat in the example)
{{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}
Use it in the stub/mock
{{now format=myformat}}
A sample templated response is -
{
"time": "{{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}{{now format=myformat}}"
}
References
How to use custom variables in Handlebars for WireMock
https://wiremock.org/studio/docs/response-templating/misc-helpers/#assignment
If you need to include the date in the ISO 8601 format, you can omit the format option:
{{now timezone='Australia/Sydney'}}
It will produce the following result: 2021-06-09T05:45:53+10:00. If you omit the timezone and just use {{now}} - it will produce a date in UTC: 2021-06-08T19:48:27Z.
For reference, you can check the RenderableDate class in wiremock
"body": "{\"datetime\": \"{{now format='yyyy-MM-dd\\'T\\'HH:mm:ss'}}\"}"

How do I convert a String to date in velocity template?

I want to convert $departureFromDate (format:yyyy-MM-dd) to date object so that I can perform increment operations on it. I have been trying to do it the following way:
#set($departureFromDate = "{{jsonPath request.body
'$.departureFromDate'}}")
#set($dateObj = $date.toDate('yyyy-MM-dd',"$departureFromDate"))
#set($calendar = $date.getCalendar())
$calendar.setTime($dateObj)
$calendar.add(6,5)
The above code works if give an actual date like:
#set($dateObj = $date.toDate('yyyy-MM-dd',"2018-09-22"))
But does not work when I try to use $departureFromDate
There are several problems in your code. First, as user7294900 noted, the right value of the first assignation seems quite weird. Then, you don't need to instanciate yourself a calendar (plus, you can write $date.calendar instead of $date.getCalendar(), and you don't need double quotes around string arguments).
#set($body = '{ "departureFromDate" : "2018-03-01" }')
$json.parse($body)
#set($departureFromDate = $json.departureFromDate)
#set($dateObj = $date.toDate('yyyy-MM-dd', $departureFromDate))
#set($calendar = $date.toCalendar($dateObj))
$calendar.add(6, 5)
The above code uses a JSON parsing tool, whose parse() method renders a json wrapper, that you shall provide in your context.
As a final advise, if you hadn't already thought of it, be sure to print $obj and $obj.class.name in your context as a trivial debugging technique if you don't understand what happens.

log4j 2 - logger.entry(p1,p2,p3) with parameter names

Is there an easy way to use logger.entry(p1,p2,p3) from log4j 2 with the parameter names included in the output?
logger.entry(p1,p2,p3)
Result:
... entry params(val1, val2, val3)
But shut result in:
... entry params(p1=val1, p2=val2, p3=val3)
Edit:
The problem with a simple solution like
logger.info(p1+" "+p2+" "+p3);
or
logger.entry("p1="+p1,"p2="+p2,"p3="+p3)
is that the string is build before every function call and results in a performance lose. I ask me, if there is a build in way in log4j.
You can try something like this:
logger.entry("p1="+p1,"p2="+p2,"p3="+p3)

FreeMarker - Remove comma from milliseconds

Using FreeMarker, I want to display a date into milliseconds:
${mydate?long}
but I get as output a comma separated millisecond:
524,354,400,000
is there any built-in function in Freemarker to remove comma ?
Thanks
It looks like as of version 2.3.17 you can use:
${myDate?long?c}
http://sourceforge.net/p/freemarker/feature-requests/72/
As an alternative you could write on your Freemarker template this directive:
<#setting number_format="computer">
This will remove all commas from numbers.
This works fine for Freemarker 2.3.23
More info about these directives can be found here:
http://freemarker.org/docs/ref_directive_setting.html
Adding to Gil's answer, if you build the configuration inside your code, you can set the flag globally by :
Configuration cfg = new Configuration();
...
cfg.setNumberFormat("computer");
Copied from the comment of the accepted answer,
In my version (2.3.26), simply ${myDate?c} will suffice, assuming that
myDate is already a long/int.
This is worked for me
Thank you

Apache commons configuration reading properties in the format a.<no>.b

I have a properties file that says
window.1.height=100
window.1.width=80
window.2.height=50
window.2.width=30
window.3.height=150
window.3.width=100
I am using the PropertiesConfiguration class and reading the properties.
How can I know the count of windows in the properties. Is therea pattern search
I usually use something like
int i = 0;
String val;
for(;;) {
val = props.get("foo" + i);
if (null == val) {
break;
}
//process val
}
This places the constraint that the counter values must be contiguous.
There are a couple of things you can do if you have any control over the properties file itself. If you are locked into that format, I don't believe there is anything you can do.
However, if you are not locked into that format, here are a couple of solutions:
XML Configuration
Change from a properties file to an XML file format. Something like this:
<windows>
<window>
<width>80</width>
<height>100</height>
</window>
<window>
<width>30</width>
<height>50</height>
</window>
<window>
<width>100</width>
<height>150</height>
</window>
</windows>
Then use XMLConfiguration instead of PropertiesConfiguration. You could then call
config.getList("windows").size()
to get the count of windows.
Properties Configuration
Your other option, which still involves a properties file, is a little bit more contrived. Your properties file would change to look like this:
window.height=100
window.width=80
window.height=50
window.width=30
window.height=150
window.width=100
Then to get the number of windows you would call
config.getList("window.height").size();
However, using this method, you would have to change how you retrieve the values. For example, in order to get the width and height of the second window, you would use this:
config.getInteger("window.width(1)");
config.getInteger("window.height(1)");
Using parens, you can access an individual element of a list, using zero-based indicies. It is a little more difficult to understand, but it would work.
The api has it already onboard. See Configuration#subset

Categories

Resources