google/flogger

Not possible to set Option.WITH_LOG_SITE?

anton-khodos opened this issue · 10 comments

Hello guys,
May be I am missing something, but I didn't find any possibility to set up Option.WITH_LOG_SITE when using FluentLogger?
Inside SimpleMessageFormatter, it seems that only format(LogData logData, SimpleLogHandler receiver) is used, which sets this option to DEFAULT.

I don't understand what you're referring to (which backend is this?).
Can you point at some code links?

I am trying to run simple log with withInjectedLogSite from the best practices (https://google.github.io/flogger/best_practice.html):

public static void testLog(String message) {
        final LogSite logSite = LogSites.callerOf(LoggingFunctions.class);
        logger.atInfo()
              .withInjectedLogSite(logSite)
              .log(message);
}

But I don't see any difference from executing:


public static void testLog(String message) {
        logger.atInfo().log(message);
}

I was expecting to see the log site in the log, or at least an option to enable it.

And regarding code links, I was refering to
SimpleMessageFormatter format method. It seems only the one the line 106 is used outside of the formatter. So option is always set to DEFAULT during formatting.

Which logger backend are you using (Log4J, JUL ..) ?
What output are you seeing?
I can't tell if:
a) you get a log site, but it's the same in both cases
b) you don't see a log site in either case

Help me understand your problem so that I can help you.

If you are referring to the code in:

This is a special case, and isn't the "normal" way that log site information should be added. Normally the backend should add log site information in a backend specific way. This formatting is just for the message that's passed to the backend, along with the rest of the information.

Currently this code is only triggered from usage inside Google, so it's right that it's not encountered in open-source code. If you are not seeing log site information being added to the logs, it's for another reason, and likely related to the choice of backend and backend configuration.

Sorry, my bad, I didn't know it was dependent on the backend.
We are using log4j as out backend.
The output of both functions is this:

2020-06-17 13:21:28.643 [INFO] org.dett.common.language.extensions.LoggingFunctions: - test1
2020-06-17 13:22:11.395 [INFO] org.dett.common.language.extensions.LoggingFunctions: - test2

The xml definition for the appender is:

<RollingFile name="logfile" fileName="C:/logs/out.log"
			 filePattern="C:/logs/out.log.%d{yyyy-MM-dd}">
	<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n"/>
	<TimeBasedTriggeringPolicy/>
</RollingFile>

Again my bad, the actual back end is slf4j

I'm afraid I don't know much about the log4J backend, that was a contributed piece of code from someone else. It's entirely possible it's not quite getting that format right, but I've got basically no experience with ICU4J myself I'm afraid.

It's using the class name, but ignoring the line number by the look of it.
I assume if you change that to print the method name, and then put the get the log site from somewhere else, you should be able to see if it's using the log site information or not.

Perhaps you can reach out to the original author.

I've looked into slf4j api and checking stack-overflow seems there is no fast solution, so will probably consider switching logger backend and just not use the log sites for now.
I would like to give sincere thanks for your help and time.