本文整理匯總了Java中org.apache.log4j.MDC.get方法的典型用法代碼示例。如果您正苦於以下問題:Java MDC.get方法的具體用法?Java MDC.get怎麽用?Java MDC.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.log4j.MDC
的用法示例。
在下文中一共展示了MDC.get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decide
import org.apache.log4j.MDC; //導入方法依賴的package包/類
/**
* Returns Filter#DENY if Expression returns false based on the values
* returned from the MDC Context.
*
* @param event
* <code>LoggingEvent</code>
*/
public int decide(final LoggingEvent event) {
int ret = DENY;
if (keys != null && expression != null) {
final List<Object> mdcValues = new ArrayList<Object>();
final List<Class<String>> keyTypes = new ArrayList<Class<String>>();
final String[] keyValues = keys.split(SPLIT_EXPRESSION);
if (keyValues != null) {
for (final String keyValue : keyValues) {
final Object mdcValue = MDC.get(keyValue);
keyTypes.add((Class<String>) keyValue.getClass());
mdcValues.add(mdcValue);
}
}
ret = evaluate(getExpressionEvaluator(keyTypes, keyValues), mdcValues);
}
return ret;
}
示例2: getMdcLong
import org.apache.log4j.MDC; //導入方法依賴的package包/類
long getMdcLong ( String key, long defval )
{
final Object o = MDC.get ( key );
if ( o == null ) return defval;
if ( o instanceof String )
{
try
{
return new Long ( o.toString () );
}
catch ( NumberFormatException x )
{
return defval;
}
}
if ( o instanceof Long )
{
return (Long)o;
}
return defval;
}
示例3: onAction
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Override
public void onAction(final Player player, final RPAction action) {
if (!action.has(TEXT)) {
return;
}
String username = PlayerEntryContainer.getContainer().get(player).username;
// remove "context" because it contains a copy of the action with the error object.
// Thus resulting a message with duplicated information that is hard to read
Object context = MDC.get("context");
MDC.remove("context");
logger.error(player.getName() + " (" + username + "):"
+ System.getProperty("line.separator")
+ action.get(TEXT).replaceAll("\r\n", System.getProperty("line.separator")));
MDC.put("context", context);
}
示例4: mdcPut
import org.apache.log4j.MDC; //導入方法依賴的package包/類
static public void mdcPut(mdcKeys key, Object value) {
LogParameters logParameters = (LogParameters) MDC.get("ContextualParameters");
if (logParameters == null) {
throw new IllegalStateException("ContextualParameters is null: call mdcInit() before!");
}
logParameters.put(key.toString().toLowerCase(), value);
}
示例5: addContext
import org.apache.log4j.MDC; //導入方法依賴的package包/類
/**
* Adds context information to Log4J's MDC object.
* The simple classname of the current object is added as the ProcessName.
* However, if a ProcessName already exists, then the simply classname is added as the SubProcessName.
*/
protected void addContext() {
String name = this.getClass().getSimpleName();
String existingName = (String) MDC.get(BusinessEventLogMeta.PROCESS_NAME);
if (existingName == null)
MDC.put(BusinessEventLogMeta.PROCESS_NAME, name);
else if (!existingName.equals(name))
MDC.put(BusinessEventLogMeta.SUB_PROCESS_NAME, name);
}
示例6: unsetContext
import org.apache.log4j.MDC; //導入方法依賴的package包/類
/**
* Unsets context information from Log4J's MDC object.
* if a SubProcessName exists, them simply remove SubProcessName from MDC
* However, if the SubProcessName doesn't exists, then remove ProcessName from MDC
*/
protected void unsetContext() {
if(MDC.get(BusinessEventLogMeta.SUB_PROCESS_NAME)!=null)
MDC.remove(BusinessEventLogMeta.SUB_PROCESS_NAME);
else
MDC.remove(BusinessEventLogMeta.PROCESS_NAME);
}
示例7: testDecide
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Test
public void testDecide() {
final String keys = "LoggedBy,MessageId";
final String expression = "LoggedBy != null || MessageId != null";
final List<Object> mdcValues = new ArrayList<Object>();
final List<Class<String>> keyTypes = new ArrayList<Class<String>>();
final String[] keyValues = keys.split(",");
if (keyValues != null) {
for (final String keyValue : keyValues) {
final Object mdcValue = MDC.get(keyValue);
keyTypes.add((Class<String>) keyValue.getClass());
mdcValues.add(mdcValue);
}
}
final ExpressionEvaluator evaluator = new ExpressionEvaluator();
evaluator.setParameters(keyValues, keyTypes.toArray(new Class[0]));
try {
evaluator.cook(expression);
} catch (Exception e) {
LOG.error(e);
fail(e.getMessage());
}
assertNotNull(evaluator);
ExpressionCache.getInstance().put(expression, evaluator);
ExpressionEvaluator testEvaluator = ExpressionCache.getInstance().get(
expression);
assertNotNull(testEvaluator);
assertTrue(testEvaluator.equals(evaluator));
ExpressionCache.getInstance().put(expression, evaluator);
}
示例8: setLoggerLevel
import org.apache.log4j.MDC; //導入方法依賴的package包/類
public static void setLoggerLevel(String loggerName, String level) {
Object o = MDC.get(Log.MDC_KEY_LOGGER_LEVELS);
Map map = new HashMap();
if (o != null && o instanceof Map) {
map.putAll((Map)o);
}
map.put(loggerName, level);
MDC.put(Log.MDC_KEY_LOGGER_LEVELS, map);
}
示例9: beforeExecute
import org.apache.log4j.MDC; //導入方法依賴的package包/類
protected void beforeExecute(Thread t, Runnable r) {
String GROUP_KEY = "GROUP_KEY";
Object group = MDC.get(GROUP_KEY);
// 將group名稱綁定到當前log4j的線程上下文中
Hashtable<?, ?> ht = MDC.getContext();
if (ht != null)
ht.clear();
if (group != null)
MDC.put(GROUP_KEY, group);
}
示例10: log
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Override
public void log(String op_cate, JSONObject jo, SecuritySession session) throws BusinessError {
try {
JSONObject t = (JSONObject)jo.clone();
t.put("op_cate", op_cate);//FIXME 變量命名,先後順序不太好弄啊規則都是反著的
//
Object remote_addr = MDC.get(LogConst.REMOTE_ADDR);
if(remote_addr != null){
t.put("remote_addr",remote_addr);
}
Object x_req = MDC.get(LogConst.X_REQ_ID);
if(x_req != null){
t.put(LogConst.X_REQ_ID,x_req);
}
if(session!=null){//並入登陸信息
if(session.company != null){
t.put("pk_company", session.company.company_id);//有可能會出錯
t.put("code_company", session.company.company_code);
t.put("name_company", session.company.company_name);
}
t.put("code_account", session.account_code);
t.put("name_account", session.account_name);
t.put("pk_account", session.account_id);
if(StringUtils.isBlank(session.ip)){//如果為空就去取來自攔截器的
t.put("ip",MDC.get(LogConst.REMOTE_ADDR));
}else{
t.put("ip", session.ip);
}
t.put("ts_crt", new Timestamp(System.currentTimeMillis()));
}
operations.save(t,getCollCode());//查詢的時候,你就費點勁
} catch (Exception e) {
ErrorBuilder.process(e);
}
}
示例11: tryIdentifyUser
import org.apache.log4j.MDC; //導入方法依賴的package包/類
private String tryIdentifyUser() {
String userName = tryGetUserName();
if (userName != null) {
return userName;
}
// NOTE: That is actually very questionable part. We're assuming ip might be in
// MDC under some hardcoded key...
Object ip = MDC.get("x_ip");
if (ip != null) {
return String.valueOf(ip);
}
return null;
}
示例12: append
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Override
public void append(LoggingEvent event) {
Object value = MDC.get(FILE_NAMES);
if (value != null) {
Collection<String> names = (Collection<String>) value;
for (String fileName : names) {
append(fileName, event);
}
}
}
示例13: append
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Override
public void append(LoggingEvent event) {
Object value = MDC.get(FILE_NAME);
if (value != null) {
append(value.toString(), event);
}
}
示例14: getMDC
import org.apache.log4j.MDC; //導入方法依賴的package包/類
/**
Returns the the context corresponding to the <code>key</code>
parameter. If there is a local MDC copy, possibly because we are
in a logging server or running inside AsyncAppender, then we
search for the key in MDC copy, if a value is found it is
returned. Otherwise, if the search in MDC copy returns a null
result, then the current thread's <code>MDC</code> is used.
<p>Note that <em>both</em> the local MDC copy and the current
thread's MDC are searched.
*/
public
Object getMDC(String key) {
Object r;
// Note the mdcCopy is used if it exists. Otherwise we use the MDC
// that is associated with the thread.
if(mdcCopy != null) {
r = mdcCopy.get(key);
if(r != null) {
return r;
}
}
return MDC.get(key);
}
示例15: newScope
import org.apache.log4j.MDC; //導入方法依賴的package包/類
@Override
public Scope newScope(TraceContext currentSpan) {
final Object previousTraceId = MDC.get("traceId");
final Object previousSpanId = MDC.get("spanId");
final Object previousParentId = MDC.get("parentId");
if (currentSpan != null) {
MDC.put("traceId", currentSpan.traceIdString());
MDC.put("spanId", HexCodec.toLowerHex(currentSpan.spanId()));
if (currentSpan.parentId() != null) {
MDC.put("parentId", HexCodec.toLowerHex(currentSpan.parentId()));
}
} else {
MDC.remove("traceId");
MDC.remove("spanId");
MDC.remove("parentId");
}
Scope scope = delegate.newScope(currentSpan);
class MDCCurrentTraceContextScope implements Scope {
@Override
public void close() {
scope.close();
if (previousTraceId != null) {
MDC.put("traceId", previousTraceId);
} else {
MDC.remove("traceId");
}
if (previousSpanId != null) {
MDC.put("spanId", previousSpanId);
} else {
MDC.remove("spanId");
}
if (previousParentId != null) {
MDC.put("parentId", previousParentId);
} else {
MDC.remove("parentId");
}
}
}
return new MDCCurrentTraceContextScope();
}