I've found this code which also uses the missing FileAppender.setFile()
method:
```
public static void setFileAppenderOutput(String filename) {

    File file = new File(filename);

    FileAppender fa = null;
    Enumeration<?> e = Logger.getRootLogger().getAllAppenders();
    while (e.hasMoreElements()) {
        Appender a = (Appender) e.nextElement();
        if ((FileAppender.class).isAssignableFrom(a.getClass())) {
            fa = (FileAppender) a;
        }
    }

    if (fa == null) {
        fa = new FileAppender();
        Layout l = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSSZZZZZ} %-5p
%c{1} %m%n");
        fa.setLayout(l);
        fa.setThreshold(Level.DEBUG);
        Logger.getRootLogger().addAppender(fa);
    }

    fa.setFile(file.getAbsolutePath());

}
```
Its purpose is to change the output file of the Root Logger's FileAppender
(we assume it has only one), or to add a new FileAppender to the Root
Logger if one does not already exist.  I can eliminate this use of
FileAppender.setFile() by rewriting the entire function for Log4j v2.  How
changing a FileAppender's output file done in v2?  I cannot find any hint
online.  I read some about Lookups, which someone mentioned, but the only
page I found was all configuration, no code.

Joel


On Wed, Aug 24, 2022 at 3:51 PM Ralph Goers <ralph.go...@dslextreme.com>
wrote:

> In Log4j 1.2 a Category was the base class for a Logger. So this would
> simply be
> looking for all the Appenders attached to the Logger. If a Category/Logger
> has no
> Appenders then it searches the parent hierarchy until it finds one. Note
> that even
> if it finds one if the Logger is configured as additive (the default) then
> the event will
> also be passed to the Logger’s parents.
>
> I haven’t looked at the code recently but I’d bet getAllAppenders only
> returns the
> Appenders directly attached to the Logger/Category.
>
> Ralph
>
> > On Aug 23, 2022, at 1:15 PM, Joel Griffith <jgrif...@nd.edu> wrote:
> >
> > I'm still trying to parse some Log4j v1 code for the update, and I hit
> > something I can't find.
> >
> > The docs here:
> >
> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html#getAllAppenders()
> > say that the Logger.getAllAppenders() method "Get[s] the appenders
> > contained in this category as an Enumeration."  There is no definition of
> > what it means for an appender to be "contained in" a category, however,
> so
> > I'm unable to predict exactly what the method will return.
> >
> > As precisely as possible, what defines the set of appenders that are
> > "contained in" a category like a logger?  In particular, does a child
> > logger contain appenders assigned to its parent?  Does a parent logger
> > contain appenders assigned to its child?
> >
> > Thanks,
> > Joel
> >
> >
> >
> >
> >
> > On Tue, Aug 16, 2022 at 3:37 AM Markus Dimmerling <
> markus.dimmerl...@ppi.de>
> > wrote:
> >
> >>> As for delaying creating the file, that can be accomplished just by
> >> specifying createOnDemand=“true”.
> >> Keep in mind, that this is not true for parent folders. Parent folders
> >> will be created on initialization even before createOnDemand. (At least
> for
> >> RollingFileAppenders)
> >>
> >> This was my workaround for that issue: A RoutingAppender wrapping the
> >> RollingFileAppender and routing all logging to null until the value is
> not
> >> empty anymore.
> >>
> >> <Routing name="parent-folder-route">
> >>  <Routes pattern="$${ctx:parent.folder}">
> >>    <Route key="$${ctx:parent.folder}">
> >>      <Null name="dontlog"/>
> >>    </Route>
> >>    <Route>
> >>      <RollingFile name="logger"
> >>                   fileName="${ctx: parent.folder}/Logging.log"
> >>                   filePattern="${ctx: parent.folder}/Logging.log.%i"
> >>                   createOnDemand="true">
> >>        ...
> >>      </RollingFile>
> >>    </Route>
> >>  </Routes>
> >> </Routing>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>
>

Reply via email to