Skip to content
Snippets Groups Projects
Commit de0ec5fc authored by felmer's avatar felmer
Browse files

LoggingContextHandler and IRemoteHostProvider moved from 'lims' to 'common'.

SVN: 3701
parent d6ee5929
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.logging;
/**
* Provider of the remote host.
*
* @author Franz-Josef Elmer
*/
public interface IRemoteHostProvider
{
public static final String UNKNOWN = "UNKNOWN";
/**
* Returns the remote (client) host.
*
* @return <code>UNKNOWN</code> if not found.
*/
public String getRemoteHost();
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.logging;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.MDC;
/**
* A class that handles the log4j context information.
*
* @author Bernd Rinn
*/
public final class LoggingContextHandler
{
private static final String MDC_KEY = "contextInfo";
private final Map<String, String> loggingContextMap = new HashMap<String, String>();
private final IRemoteHostProvider remoteHostProvider;
public LoggingContextHandler(IRemoteHostProvider remoteHostProvider)
{
this.remoteHostProvider = remoteHostProvider;
}
/**
* Adds specified context.
*/
public void addContext(final String contextID, final String context)
{
loggingContextMap.put(contextID, context);
}
/** Destroys the logging context information for the session identified by <var>contextID</var>. */
public final void destroyContext(String contextID)
{
loggingContextMap.remove(contextID);
}
/** Sets the logging context information for the <var>contextID</var> in the {@link MDC}. */
public final void setMDC(String contextID)
{
final String context = loggingContextMap.get(contextID);
String remoteHost = remoteHostProvider.getRemoteHost();
if (context == null)
{
MDC.put(MDC_KEY, String.format(" {UNKNOWN_TOKEN='%s', ip=%s}", contextID, remoteHost));
} else
{
MDC.put(MDC_KEY, String.format(" {%s, ip=%s}", context, remoteHost));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment