You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Asma Zinneera Jabir <az...@gmail.com> on 2018/01/09 18:28:57 UTC

Log4J2 Lookup working when run on IDE but not when run in the command line

I am using the Routing Appender in Log4J2 to route to different log files.
The path is determined by a custom LookUp. The configuration and the lookup
are as follows.

*Log4j2.xml Routing appender*

<Routing name="Routing">
   <Routes pattern="$${path:key2}">
       <Route>
          <RollingFile name="ABC_LOGFILE"
fileName="${sys:abc.home}/${path:key2}.log"

 filePattern="${sys:abc.home}/${path:key2}-%d{MM-dd-yyyy}-%i.log">
             <PatternLayout pattern="[%d] %5p {%c} - %m%ex%n" />
             <Policies>
                <SizeBasedTriggeringPolicy size="50 MB" />
             </Policies>
             <DefaultRolloverStrategy max="100" />
          </RollingFile>
       </Route>
   </Routes>
</Routing>

*Custom Lookup*

@Plugin(name = "path", category = StrLookup.CATEGORY)
public class LogFilePathLookup implements StrLookup {
    private static String logFilePath = "abc";

    @Override
    public String lookup(String key) {
        return null;
    }

    @Override
    public String lookup(LogEvent logEvent, String key) {
        return logFilePath;
    }

    /**
     * Sets the log file path.
     *
     * @param logFilePath log file path
     */
    public static void setLogFilePath(String logFilePath) {
        LogFilePathLookup.logFilePath = logFilePath;
    }
}

This works perfectly when I run on the IDE (Intellij IDEA) with the VM
options set as
-Dlog4j.configurationFile=/path/to/log4j2.xml
-Dabc.home=/home/asma/abchome

and some program arguments. But when the same is run in the terminal as

java -Dlog4j.configurationFile=/path/to/log4j2.xml
-Dabc.home=/home/asma/abchome -jar *<program_arguments>*

the name of the log file getting created is ${path:key2}.log, meaning it is
not replaced by the lookup String value. What could be the reason for this?



Regards,
Asma

Re: Log4J2 Lookup working when run on IDE but not when run in the command line

Posted by Asma Zinneera Jabir <az...@gmail.com>.
 Hi Matt,

Thank you for your response. I figured out the issue. Had to add the
"packages" attribute in the <Configuration> element which I had not added
before.



On Wed, Jan 10, 2018 at 3:37 AM, Ralph Goers <ra...@dslextreme.com>
wrote:

> Shoot. Replied to the wrong email.
>
> Sent from my iPhone
>
> > On Jan 9, 2018, at 3:00 PM, Ralph Goers <ra...@dslextreme.com>
> wrote:
> >
> > Maybe, but the point is that the batch is managed by the appender, so
> there is no need for a new method in the Appender interface.
> >
> > Ralph
> >
> >> On Jan 9, 2018, at 2:20 PM, Matt Sicker <bo...@gmail.com> wrote:
> >>
> >> A common source of problems with custom plugins is related to the
> >> Log4j2Plugins.dat file not being generated or being included on the
> >> classpath. If you shade everything into a single jar, for example, you
> need
> >> to use a custom shade rule to merge the plugin dat files (I've seen some
> >> shade plugins include this feature natively for log4j2 specifically).
> >>
> >> If you could share more info about how you build your application, that
> >> would be super helpful!
> >>
> >>> On 9 January 2018 at 12:28, Asma Zinneera Jabir <az...@gmail.com>
> wrote:
> >>>
> >>> I am using the Routing Appender in Log4J2 to route to different log
> files.
> >>> The path is determined by a custom LookUp. The configuration and the
> lookup
> >>> are as follows.
> >>>
> >>> *Log4j2.xml Routing appender*
> >>>
> >>> <Routing name="Routing">
> >>>  <Routes pattern="$${path:key2}">
> >>>      <Route>
> >>>         <RollingFile name="ABC_LOGFILE"
> >>> fileName="${sys:abc.home}/${path:key2}.log"
> >>>
> >>> filePattern="${sys:abc.home}/${path:key2}-%d{MM-dd-yyyy}-%i.log">
> >>>            <PatternLayout pattern="[%d] %5p {%c} - %m%ex%n" />
> >>>            <Policies>
> >>>               <SizeBasedTriggeringPolicy size="50 MB" />
> >>>            </Policies>
> >>>            <DefaultRolloverStrategy max="100" />
> >>>         </RollingFile>
> >>>      </Route>
> >>>  </Routes>
> >>> </Routing>
> >>>
> >>> *Custom Lookup*
> >>>
> >>> @Plugin(name = "path", category = StrLookup.CATEGORY)
> >>> public class LogFilePathLookup implements StrLookup {
> >>>   private static String logFilePath = "abc";
> >>>
> >>>   @Override
> >>>   public String lookup(String key) {
> >>>       return null;
> >>>   }
> >>>
> >>>   @Override
> >>>   public String lookup(LogEvent logEvent, String key) {
> >>>       return logFilePath;
> >>>   }
> >>>
> >>>   /**
> >>>    * Sets the log file path.
> >>>    *
> >>>    * @param logFilePath log file path
> >>>    */
> >>>   public static void setLogFilePath(String logFilePath) {
> >>>       LogFilePathLookup.logFilePath = logFilePath;
> >>>   }
> >>> }
> >>>
> >>> This works perfectly when I run on the IDE (Intellij IDEA) with the VM
> >>> options set as
> >>> -Dlog4j.configurationFile=/path/to/log4j2.xml
> >>> -Dabc.home=/home/asma/abchome
> >>>
> >>> and some program arguments. But when the same is run in the terminal as
> >>>
> >>> java -Dlog4j.configurationFile=/path/to/log4j2.xml
> >>> -Dabc.home=/home/asma/abchome -jar *<program_arguments>*
> >>>
> >>> the name of the log file getting created is ${path:key2}.log, meaning
> it is
> >>> not replaced by the lookup String value. What could be the reason for
> this?
> >>>
> >>>
> >>>
> >>> Regards,
> >>> Asma
> >>>
> >>
> >>
> >>
> >> --
> >> Matt Sicker <bo...@gmail.com>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Log4J2 Lookup working when run on IDE but not when run in the command line

Posted by Ralph Goers <ra...@dslextreme.com>.
Shoot. Replied to the wrong email.

Sent from my iPhone

> On Jan 9, 2018, at 3:00 PM, Ralph Goers <ra...@dslextreme.com> wrote:
> 
> Maybe, but the point is that the batch is managed by the appender, so there is no need for a new method in the Appender interface.
> 
> Ralph
> 
>> On Jan 9, 2018, at 2:20 PM, Matt Sicker <bo...@gmail.com> wrote:
>> 
>> A common source of problems with custom plugins is related to the
>> Log4j2Plugins.dat file not being generated or being included on the
>> classpath. If you shade everything into a single jar, for example, you need
>> to use a custom shade rule to merge the plugin dat files (I've seen some
>> shade plugins include this feature natively for log4j2 specifically).
>> 
>> If you could share more info about how you build your application, that
>> would be super helpful!
>> 
>>> On 9 January 2018 at 12:28, Asma Zinneera Jabir <az...@gmail.com> wrote:
>>> 
>>> I am using the Routing Appender in Log4J2 to route to different log files.
>>> The path is determined by a custom LookUp. The configuration and the lookup
>>> are as follows.
>>> 
>>> *Log4j2.xml Routing appender*
>>> 
>>> <Routing name="Routing">
>>>  <Routes pattern="$${path:key2}">
>>>      <Route>
>>>         <RollingFile name="ABC_LOGFILE"
>>> fileName="${sys:abc.home}/${path:key2}.log"
>>> 
>>> filePattern="${sys:abc.home}/${path:key2}-%d{MM-dd-yyyy}-%i.log">
>>>            <PatternLayout pattern="[%d] %5p {%c} - %m%ex%n" />
>>>            <Policies>
>>>               <SizeBasedTriggeringPolicy size="50 MB" />
>>>            </Policies>
>>>            <DefaultRolloverStrategy max="100" />
>>>         </RollingFile>
>>>      </Route>
>>>  </Routes>
>>> </Routing>
>>> 
>>> *Custom Lookup*
>>> 
>>> @Plugin(name = "path", category = StrLookup.CATEGORY)
>>> public class LogFilePathLookup implements StrLookup {
>>>   private static String logFilePath = "abc";
>>> 
>>>   @Override
>>>   public String lookup(String key) {
>>>       return null;
>>>   }
>>> 
>>>   @Override
>>>   public String lookup(LogEvent logEvent, String key) {
>>>       return logFilePath;
>>>   }
>>> 
>>>   /**
>>>    * Sets the log file path.
>>>    *
>>>    * @param logFilePath log file path
>>>    */
>>>   public static void setLogFilePath(String logFilePath) {
>>>       LogFilePathLookup.logFilePath = logFilePath;
>>>   }
>>> }
>>> 
>>> This works perfectly when I run on the IDE (Intellij IDEA) with the VM
>>> options set as
>>> -Dlog4j.configurationFile=/path/to/log4j2.xml
>>> -Dabc.home=/home/asma/abchome
>>> 
>>> and some program arguments. But when the same is run in the terminal as
>>> 
>>> java -Dlog4j.configurationFile=/path/to/log4j2.xml
>>> -Dabc.home=/home/asma/abchome -jar *<program_arguments>*
>>> 
>>> the name of the log file getting created is ${path:key2}.log, meaning it is
>>> not replaced by the lookup String value. What could be the reason for this?
>>> 
>>> 
>>> 
>>> Regards,
>>> Asma
>>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <bo...@gmail.com>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Log4J2 Lookup working when run on IDE but not when run in the command line

Posted by Ralph Goers <ra...@dslextreme.com>.
Maybe, but the point is that the batch is managed by the appender, so there is no need for a new method in the Appender interface.

Ralph

> On Jan 9, 2018, at 2:20 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> A common source of problems with custom plugins is related to the
> Log4j2Plugins.dat file not being generated or being included on the
> classpath. If you shade everything into a single jar, for example, you need
> to use a custom shade rule to merge the plugin dat files (I've seen some
> shade plugins include this feature natively for log4j2 specifically).
> 
> If you could share more info about how you build your application, that
> would be super helpful!
> 
>> On 9 January 2018 at 12:28, Asma Zinneera Jabir <az...@gmail.com> wrote:
>> 
>> I am using the Routing Appender in Log4J2 to route to different log files.
>> The path is determined by a custom LookUp. The configuration and the lookup
>> are as follows.
>> 
>> *Log4j2.xml Routing appender*
>> 
>> <Routing name="Routing">
>>   <Routes pattern="$${path:key2}">
>>       <Route>
>>          <RollingFile name="ABC_LOGFILE"
>> fileName="${sys:abc.home}/${path:key2}.log"
>> 
>> filePattern="${sys:abc.home}/${path:key2}-%d{MM-dd-yyyy}-%i.log">
>>             <PatternLayout pattern="[%d] %5p {%c} - %m%ex%n" />
>>             <Policies>
>>                <SizeBasedTriggeringPolicy size="50 MB" />
>>             </Policies>
>>             <DefaultRolloverStrategy max="100" />
>>          </RollingFile>
>>       </Route>
>>   </Routes>
>> </Routing>
>> 
>> *Custom Lookup*
>> 
>> @Plugin(name = "path", category = StrLookup.CATEGORY)
>> public class LogFilePathLookup implements StrLookup {
>>    private static String logFilePath = "abc";
>> 
>>    @Override
>>    public String lookup(String key) {
>>        return null;
>>    }
>> 
>>    @Override
>>    public String lookup(LogEvent logEvent, String key) {
>>        return logFilePath;
>>    }
>> 
>>    /**
>>     * Sets the log file path.
>>     *
>>     * @param logFilePath log file path
>>     */
>>    public static void setLogFilePath(String logFilePath) {
>>        LogFilePathLookup.logFilePath = logFilePath;
>>    }
>> }
>> 
>> This works perfectly when I run on the IDE (Intellij IDEA) with the VM
>> options set as
>> -Dlog4j.configurationFile=/path/to/log4j2.xml
>> -Dabc.home=/home/asma/abchome
>> 
>> and some program arguments. But when the same is run in the terminal as
>> 
>> java -Dlog4j.configurationFile=/path/to/log4j2.xml
>> -Dabc.home=/home/asma/abchome -jar *<program_arguments>*
>> 
>> the name of the log file getting created is ${path:key2}.log, meaning it is
>> not replaced by the lookup String value. What could be the reason for this?
>> 
>> 
>> 
>> Regards,
>> Asma
>> 
> 
> 
> 
> -- 
> Matt Sicker <bo...@gmail.com>



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Log4J2 Lookup working when run on IDE but not when run in the command line

Posted by Matt Sicker <bo...@gmail.com>.
A common source of problems with custom plugins is related to the
Log4j2Plugins.dat file not being generated or being included on the
classpath. If you shade everything into a single jar, for example, you need
to use a custom shade rule to merge the plugin dat files (I've seen some
shade plugins include this feature natively for log4j2 specifically).

If you could share more info about how you build your application, that
would be super helpful!

On 9 January 2018 at 12:28, Asma Zinneera Jabir <az...@gmail.com> wrote:

> I am using the Routing Appender in Log4J2 to route to different log files.
> The path is determined by a custom LookUp. The configuration and the lookup
> are as follows.
>
> *Log4j2.xml Routing appender*
>
> <Routing name="Routing">
>    <Routes pattern="$${path:key2}">
>        <Route>
>           <RollingFile name="ABC_LOGFILE"
> fileName="${sys:abc.home}/${path:key2}.log"
>
>  filePattern="${sys:abc.home}/${path:key2}-%d{MM-dd-yyyy}-%i.log">
>              <PatternLayout pattern="[%d] %5p {%c} - %m%ex%n" />
>              <Policies>
>                 <SizeBasedTriggeringPolicy size="50 MB" />
>              </Policies>
>              <DefaultRolloverStrategy max="100" />
>           </RollingFile>
>        </Route>
>    </Routes>
> </Routing>
>
> *Custom Lookup*
>
> @Plugin(name = "path", category = StrLookup.CATEGORY)
> public class LogFilePathLookup implements StrLookup {
>     private static String logFilePath = "abc";
>
>     @Override
>     public String lookup(String key) {
>         return null;
>     }
>
>     @Override
>     public String lookup(LogEvent logEvent, String key) {
>         return logFilePath;
>     }
>
>     /**
>      * Sets the log file path.
>      *
>      * @param logFilePath log file path
>      */
>     public static void setLogFilePath(String logFilePath) {
>         LogFilePathLookup.logFilePath = logFilePath;
>     }
> }
>
> This works perfectly when I run on the IDE (Intellij IDEA) with the VM
> options set as
> -Dlog4j.configurationFile=/path/to/log4j2.xml
> -Dabc.home=/home/asma/abchome
>
> and some program arguments. But when the same is run in the terminal as
>
> java -Dlog4j.configurationFile=/path/to/log4j2.xml
> -Dabc.home=/home/asma/abchome -jar *<program_arguments>*
>
> the name of the log file getting created is ${path:key2}.log, meaning it is
> not replaced by the lookup String value. What could be the reason for this?
>
>
>
> Regards,
> Asma
>



-- 
Matt Sicker <bo...@gmail.com>