Hi Daniel,

You can obtain the LoggerContext via SLF4J's logger factory. Here is the code.

 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

Once you have the LoggerContext you can retrieve its properties. If this is not an option, as of logback 1.0.0, properties are scoped. Properties can be in local scope, context scope and in system scope.
See [1].

In a logback.xml config file, you can define a property in system scope as follows:

<configuration>
 <property scope="system" name="worker.interval" value="101" />
 ...
</configuration>

The system property can be retrieved in the usual way:

  String workerIntervalAsStr = System.getProperty("worker.interval");


HTH,

--
Ceki
http://twitter.com/#!/ceki

[1] http://logback.qos.ch/manual/configuration.html#variableSubstitution


On 10.02.2012 16:49, Daniel Ferber wrote:
Hi Ceki,

Sorry I was not able to explain my intention. Here is an example.

A worker class iterates over data until a result is found. The worker
allows to register a callback that executes nextIteration() on each
iteration. The callback logs some properties that describe the progress
of the worker. But writes log only on every “interval” calls, to prevent
cluttering the log, since the worker will execute a million of iterations.


Worker w = new Worker().
w.addCallback(new ContinuousCallback()) {

int counter= 0;

void nextIteration() {

int interval = Integer.parseInt(logger.getProperty(“worker.interval”));

if (logger.isInforEnabled() && counter++ % interval == 0) {

// collect attributesArray

logger.info <http://logger.info>(“i={}, convergence={}, ... ",
attributesArray) ;

}

}
}

I would like to get the “interval” from the log configuration (eg, for
logback):

<configuration>

<property name=“worker.interval” value=“100” />

<logger name="callback" level="INFO" />

</configuration>

I think that “interval” is a property that controls how data is logged
and I think it would be more elegant being able to set the property
together with the logger configuration.

There seems that I can get properties from a LoggerContext withing
logback, but I understand that slf4j does not exposes this context.

Best regards,

Daniel Felix Ferber


2012/2/9 ceki <[email protected] <mailto:[email protected]>>


    Hi Daniel,

    I don't follow you. Could you provide an *example* of what you have
    in mind (assume you have complete liberty with the SLF4J API) ?

    --
    Ceki
    http://twitter.com/#!/ceki <http://twitter.com/#%21/ceki>


    On 09.02.2012 14:45, Daniel Felix Ferber wrote:

        Hi all,

        I was wondering about slf4j being able to get properties defined
        within the
        configuration file form the logger framework (eg logback or
        log4j). Would that
        be a reasonable use for slf4j?

        1) I would configure the granularity (amount of content) of a
        log message that
        reports the result of a long-running execution.

        2) Also, I would configure the number of step between two log
        messages on a
        long-running task.

        For the first case, I could define several loggers (a simple
        one, a detailed
        one). I don't think I should use two levels, since the report
        cannot be
        considered 'info', not 'debug' or 'trace'.

        For the second case, I am not sure how to handle it. Currently
        the application
        has a configuration property that defines how log will behave
        (number of steps,
        amount of details). But taking it rigorously, these are not
        application
        configuration, but logger configuration.

        Best regards,
        Daniel Felix Ferber


_______________________________________________
slf4j-user mailing list
[email protected]
http://mailman.qos.ch/mailman/listinfo/slf4j-user

Reply via email to