On May 31, 2010, at 1:43 PM, Thorbjørn Ravn Andersen wrote:
> Den 30/05/10 21.34, Ralph Goers skrev:
>> Wouldn't it make more sense for the LoggerContext to have a reference to the
>> ServletContext? The Appender could then do
>>
>> if (getContext().getServletContext() != null) {
>> getContext().getServletContext().log(event.getFormattedMessage());
>> }
>>
>>
> I do not know. I am unfamiliar with what the LoggerContext provides. Are
> you suggesting that for each and every possible sub-context you will provide
> a field in the LoggerContext?
No. It would probably make more sense to do what JSF does when it is running in
a Portal:
Object obj = getContext().getExternalContext();
if (obj != null && obj instanceof ServletContext) {
((ServletContext) obj).log(event.getFormattedMessage());
}
One could also do:
if (getContext() instanceof LoggerServletContext) {
((LoggerServletContext)
getContext()).getContext().log(event.getFormattedMessage());
}
in which case LoggerServletContext would extend LoggerContext and provide the
field.
>
>> Note that if the servlet adds its name to the MDC then all log records will
>> have this available.
>>
>> To be honest though, I would have expected the desire would be to have the
>> ServletContext's log methods route to Log4j, not the other way around.
>>
> The reason is simple. There is no guarantee that any J2EE container can or
> will allow access to the filesystem. Using the servlet log-method is the
> _ONLY_ well-defined way to log things inside a J2EE container without needing
> to make assumptions and manual configurations (remember the container doesn't
> help).
>
> Possible assumptions may be:
>
> * I can access the file system.
> * Current working directory can be used (for writing or starting navigation)
> * The JVM provides environment variable which can be used to locate the
> user.home of the operating system user running the JVM.
> * The JVM is allowed to write in user.home.
>
> I like debug logs placed in the filesystem, but I also like to be able to
> send log messages to the standard log mechanism. The latter I cannot do
> right now.
>
OK. But just because you can't write to the FileSystem doesn't mean that
writing via the ServletContext's log method is your only option. You can write
to a database, syslog, SNMP, SMTP, etc. But yes, some configuration would be
required to make that happen. Writing to the containers log file is sub-optimal
in a lot of cases for various reasons such as; a) writing debug logs to the
local file system can be a security risk, and b) multiple webapps may end up in
a co-mingled log file. While I understand your use case it is just one I would
rarely, if ever, recommend.
Ralph
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]