[logback-user] How to know the filename used by the appender of a given logger
Ulrich
Forums at gombers.de
Thu Aug 1 08:57:16 CEST 2013
I see what you mean; one logger could use several appenders and each
appender may or may not write to file. I thought to test for the file
(so I was aware of this) but didn't think of the fact that the appenders
introduce another layer of abstraction. So I would have to retrieve each
appender to get the filename(s). And in case it was a
RollingFileAppender, the interesting message might just have been rolled
off.
My idea was to send a mail when a special event rises and to indicate
the message file for help. The person fixing the problem might not be
the one maintaining the logging environment; it was meant to provide the
shortcut to help problem researching.
So I have either to think of another approach or just leave it as it is.
Thank you for your hint: It helps me for a better understanding of the
logback environent. But as I said yesterday; I don't want to limit the
logging opportunities for the operators.
THanks a lot,
Ulrich
Am 31.07.2013 16:39, schrieb David Roussel:
> Ulrich,
>
>> I thought logback would know the file it uses - why not supply the name?
>
> There could be no file (console) or a hundred files, so it's not
> possible to generalise.
>
> Try something like this:
>
> <configuration>
> <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
> <file>mylog.txt</file>
>
> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
> <!-- rollover daily -->
> <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
> <timeBasedFileNamingAndTriggeringPolicy
> class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
> <!-- or whenever the file size reaches 100MB -->
> <maxFileSize>100MB</maxFileSize>
> </timeBasedFileNamingAndTriggeringPolicy>
> </rollingPolicy>
> <encoder>
> <pattern>%msg%n</pattern>
> </encoder>
> </appender>
>
>
> <root level="DEBUG">
> <appender-ref ref="ROLLING" />
> </root>
>
> </configuration>
>
> This example is directly from http://logback.qos.ch/manual/appenders.html
>
> But instead SizeAndTimeBasedFNATP, you should
> subclass SizeAndTimeBasedFNATP and override the getActiveFileName()
> method. Then capture the filename, and put it into somewhere your app
> can see.
>
> There might be other ways, but I think this approach would work.
>
> David
>
>
>
> On 31 Jul 2013, at 13:41, Ulrich <Forums at gombers.de
> <mailto:Forums at gombers.de>> wrote:
>
>> Thank you. I don't want to set the filename or maintain the appender
>> from my program. This is up to the operators running the application.
>> And I don't want to add limitations beyond those given by logback
>> itself. I thought logback would know the file it uses - why not supply
>> the name?
>> I could also (and I do it already) set the logfile by the operationg
>> system and provide the name as an argument. In this case the I must
>> request that only the console-appender can be used. But I don't like
>> this approach.
>>
>> Ulrich
>>
>>
>> Am 31.07.2013 12:21, schrieb David Roussel:
>>> I've made my app know the logger file name so that I can put a link
>>> to it in an email.
>>>
>>> Buy the way I did it was to make the app choose the filename and set
>>> a system property. Then in logback.xml refer to the system property.
>>>
>>> If that approach does not work, because you are using a dynamic log
>>> file name, then I suggest you try subclassing the rolling appender
>>> and getting the new filename each time one is generated, and then
>>> putting that value into a static singleton that is visible to your app.
>>>
>>> David
>>>
>>> On 31 Jul 2013, at 11:12, Ulrich <Forums at gombers.de
>>> <mailto:Forums at gombers.de>> wrote:
>>>
>>>>
>>>> Within my application I would like to know the filename used by it's
>>>> logger. I've asked this question before but didn't get an answer. So
>>>> I will give it another try.
>>>> I've browsed through the Javadoc of the classes and interfaces where
>>>> I thought by their name they should be able to provide this piece of
>>>> information (LoggerContext and Appender to name two of them), but
>>>> wasn't lucky.
>>>>
>>>> Can anyone help here?
>>>> Ulrich
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Logback-user mailing list
>>>> Logback-user at qos.ch <mailto:Logback-user at qos.ch>
>>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>> _______________________________________________
>>> Logback-user mailing list
>>> Logback-user at qos.ch <mailto:Logback-user at qos.ch>
>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch <mailto:Logback-user at qos.ch>
>> http://mailman.qos.ch/mailman/listinfo/logback-user
>
>
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>
More information about the Logback-user
mailing list