本文整理汇总了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();
}