Forum Discussion
Hi -- Yes, I'd seen that post, and that's where I learned about setting up the custom logger. What I was missing was pretty simple -- how to get groovy assert results into that file. I needed to put the assert into a try/catch, like this:
import org.apache.log4j.Logger def fileLogger = Logger.getLogger("ASSERTION.SCRIPT.LOGGER"); def String expected = "Some Text" def String actual = "Some Unexpected Text" assertVals(actual, expected, fileLogger) def assertVals(String actual, String expected, Logger logger) { if (expected != null) { try { assert expected == actual } catch(AssertionError e) { logger.info(e.getMessage()) } } }
Two notes:
- If you want the "power assert" formatting in the file, leave it as above. If you want a more standard version on a single line, add the optional ": 'This is the result'" part after the assert statement. Not sure why that is, but it works.
- Because the "power assert formatting is sensitive, I turned off the formatting in the layout of the appender.
<layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%m%n"/> </layout>
Of course, when you do that you don't get useful stuff like date/time.
Thanks,
If you are looking for date and all you need provide the appropriate value in the configuration for ConversionPattern.
You may try something like below
%d{dd-MMM-yyyy HH:mm:ss,SSS} [%-8t] %-5p %c{1}:%L %x - %m %n
- davidp_110 years agoOccasional Contributor
The reason I mention it is because if you include the date, and you elect to go with the groovy power assert formatting, it won't look right (those lines won't line up correctly).
Instead of this:
assert expected == actual | | | Some Text| Some Unexpected Text false
You'll get this:
2015-12-01 23:05:26,655 INFO [LOGGER] assert expected == actual | | | Some Text| Some Unexpected Text false
- nmrao10 years ago
Champion Level 1
May be a small hack, if that is ok, by changing
logger.info(e.getMessage())
to
logger.info('\n'+e.getMessage())- davidp_110 years agoOccasional Contributor
Thanks!
I have a different hack, though. I changed the layout conversion pattern parameters in the log4j appender definition to include the date plus an extra line break (%n parameter), like this:
<layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd-MMM-yyyy HH:mm:ss} %n%m%n"/> </layout>
With the results looking like this:
02-Dec-2015 09:26:17 assert expected == actual | | | Some Text| Some Unexpected Text false