Being frustrated with the date format of the `Logb...
# box-products
r
Being frustrated with the date format of the
Logbox
file appender I thought I'd rustle up my own layout, simples? So I've created my custom layout and referenced it in config and all's well... ...except I'm having trouble replicating, as a first pass, the default format. The appender name doesn't appear to available via the
LogEvent
object and I can't seem to use the
severityToString()
helper. Also I'd like to control the first row, csv column names, but not sure how to replace that line. Thought or further read suggestions welcome.
b
@richard.herbert The header line can't be controlled, but it probably should be. I think there's a ticket for that.
The default layout logic in the file appender is
Copy code
if ( hasCustomLayout() ) {
			entry = getCustomLayout().format( loge );
		} else {
			// Cleanup main message
			if ( len( loge.getExtraInfoAsString() ) ) {
				message = message & " | ExtraInfo:" & loge.getExtraInfoAsString();
			}
			message = replace( message, """", """""", "all" );
			message = replace( message, "#chr( 13 )##chr( 10 )#", "  ", "all" );
			message = replace( message, chr( 13 ), "  ", "all" );

			// Entry string
			entry = """#severityToString( logEvent.getSeverity() )#"",""#getname()#"",""#dateFormat( timestamp, "mm/dd/yyyy" )#"",""#timeFormat( timestamp, "HH:mm:ss" )#"",""#loge.getCategory()#"",""#message#""";
		}
so if you put what 's in the second half of that if in your custom layout, you'll have the default layout
So, the category name comes from
getExtraInfoAsString()
on the log event object, so inside your custom layout, it would be
loge.getExtraInfoAsString()
, etc.
The logger name is the category from logevent
The appender name doesn't appear to available via the
LogEvent
object
I'm curious what you mean here. Are you wanting to know if your custom appender was called from a
FileAppender
,
RollingFileAPpender
, etc? Generally speaking, that should be none of its concern.
Unless you were hopping to have the same generic formatter used across several appenders with slightly different behavior. You'd need more than one formatter class for that, but you could use inheritance to aid in code reuse for the polymorphism if you needed.
r
I can see now that that first row is currently hardcoded into the
FileAppender
so I guess I could make my own appender by copying the existing and change it to meet my needs. It should really have an
ExtraInfo
entry to support that from
debug()
etc. Category name is fine, I can get that from
#logEvent.getCategory()#
, it's the appender name I can't reach. I guess that's a mute point, I could just hardcode it in my return string. I did have
#severityToString( logEvent.getSeverity() )#
in my layout but that seems to fails. I'm not sure how that method makes it's way into the layout?
b
Ahh, yes, you should put in a ticket to improve that. Perhaps just passing the appender itself to the formatter would be handy since that method exists on the base abstract appender
Copy code
/**
	 * convert a severity to a string
	 *
	 * @severity The severity to convert to a string
	 */
	function severityToString( required numeric severity ){
		return this.logLevels.lookup( arguments.severity );
	}
You can probably just create an instance of the abstract appender to call it, or honestly, just create the logleves class directly since that's really where it lives
r
Happy to raise a couple of tickets. Just wanted to make sure that I hadn't missed something in my understanding of
Logbox
- thanks for the insight.
b
You're one of the few people to ever use a custom appender layout, so don't assume this ground is that well covered 🙂
r
I can't believe I'm the only pedant, outside the USA, who's annoyed by the "mm/dd/yyyy" date formatting 🙂
b
lol
My usual reason for customizing stuff is because I hate how verbose the default exception serialization can be. But I normally just end up making a custom appender
r
That looks like the road I'm heading down.