📄 loggingevent.java
字号:
} return ndc; } /** 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); } /** Obtain a copy of this thread's MDC prior to serialization or asynchronous logging. */ public void getMDCCopy() { if(mdcCopyLookupRequired) { mdcCopyLookupRequired = false; // the clone call is required for asynchronous logging. // See also bug #5932. Hashtable t = (Hashtable) MDC.getContext(); if(t != null) { mdcCopy = (Hashtable) t.clone(); } } } public String getRenderedMessage() { if(renderedMessage == null && message != null) { if(message instanceof String) renderedMessage = (String) message; else { LoggerRepository repository = logger.getLoggerRepository(); if(repository instanceof RendererSupport) { RendererSupport rs = (RendererSupport) repository; renderedMessage= rs.getRendererMap().findAndRender(message); } else { renderedMessage = message.toString(); } } } return renderedMessage; } /** Returns the time when the application started, in milliseconds elapsed since 01.01.1970. */ public static long getStartTime() { return startTime; } public String getThreadName() { if(threadName == null) threadName = (Thread.currentThread()).getName(); return threadName; } /** Returns the throwable information contained within this event. May be <code>null</code> if there is no such information. <p>Note that the {@link Throwable} object contained within a {@link ThrowableInformation} does not survive serialization. @since 1.1 */ public ThrowableInformation getThrowableInformation() { return throwableInfo; } /** Return this event's throwable's string[] representaion. */ public String[] getThrowableStrRep() { if(throwableInfo == null) return null; else return throwableInfo.getThrowableStrRep(); } private void readLevel(ObjectInputStream ois) throws java.io.IOException, ClassNotFoundException { int p = ois.readInt(); try { String className = (String) ois.readObject(); if(className == null) { level = Level.toLevel(p); } else { Method m = (Method) methodCache.get(className); if(m == null) { Class clazz = Loader.loadClass(className); // Note that we use Class.getDeclaredMethod instead of // Class.getMethod. This assumes that the Level subclass // implements the toLevel(int) method which is a // requirement. Actually, it does not make sense for Level // subclasses NOT to implement this method. Also note that // only Level can be subclassed and not Priority. m = clazz.getDeclaredMethod(TO_LEVEL, TO_LEVEL_PARAMS); methodCache.put(className, m); } PARAM_ARRAY[0] = new Integer(p); level = (Level) m.invoke(null, PARAM_ARRAY); } } catch(Exception e) { LogLog.warn("Level deserialization failed, reverting to default.", e); level = Level.toLevel(p); } } private void readObject(ObjectInputStream ois) throws java.io.IOException, ClassNotFoundException { ois.defaultReadObject(); readLevel(ois); // Make sure that no location info is available to Layouts if(locationInfo == null) locationInfo = new LocationInfo(null, null); } private void writeObject(ObjectOutputStream oos) throws java.io.IOException { // Aside from returning the current thread name the wgetThreadName // method sets the threadName variable. this.getThreadName(); // This sets the renders the message in case it wasn't up to now. this.getRenderedMessage(); // This call has a side effect of setting this.ndc and // setting ndcLookupRequired to false if not already false. this.getNDC(); // This call has a side effect of setting this.mdcCopy and // setting mdcLookupRequired to false if not already false. this.getMDCCopy(); // This sets the throwable sting representation of the event throwable. this.getThrowableStrRep(); oos.defaultWriteObject(); // serialize this event's level writeLevel(oos); } private void writeLevel(ObjectOutputStream oos) throws java.io.IOException { oos.writeInt(level.toInt()); Class clazz = level.getClass(); if(clazz == Level.class) { oos.writeObject(null); } else { // writing directly the Class object would be nicer, except that // serialized a Class object can not be read back by JDK // 1.1.x. We have to resort to this hack instead. oos.writeObject(clazz.getName()); } } /** * Set value for MDC property. * This adds the specified MDC property to the event. * Access to the MDC is not synchronized, so this * method should only be called when it is known that * no other threads are accessing the MDC. * @since 1.2.15 * @param propName * @param propValue */ public final void setProperty(final String propName, final String propValue) { if (mdcCopy == null) { getMDCCopy(); } if (mdcCopy == null) { mdcCopy = new Hashtable(); } mdcCopy.put(propName, propValue); } /** * Return a property for this event. The return value can be null. * * Equivalent to getMDC(String) in log4j 1.2. Provided * for compatibility with log4j 1.3. * * @param key property name * @return property value or null if property not set * @since 1.2.15 */ public final String getProperty(final String key) { Object value = getMDC(key); String retval = null; if (value != null) { retval = value.toString(); } return retval; } /** * Check for the existence of location information without creating it * (a byproduct of calling getLocationInformation). * @return true if location information has been extracted. * @since 1.2.15 */ public final boolean locationInformationExists() { return (locationInfo != null); } /** * Getter for the event's time stamp. The time stamp is calculated starting * from 1970-01-01 GMT. * @return timestamp * * @since 1.2.15 */ public final long getTimeStamp() { return timeStamp; } /** * Returns the set of the key values in the properties * for the event. * * The returned set is unmodifiable by the caller. * * Provided for compatibility with log4j 1.3 * * @return Set an unmodifiable set of the property keys. * @since 1.2.15 */ public Set getPropertyKeySet() { return getProperties().keySet(); } /** * Returns the set of properties * for the event. * * The returned set is unmodifiable by the caller. * * Provided for compatibility with log4j 1.3 * * @return Set an unmodifiable map of the properties. * @since 1.2.15 */ public Map getProperties() { getMDCCopy(); Map properties; if (mdcCopy == null) { properties = new HashMap(); } else { properties = mdcCopy; } return Collections.unmodifiableMap(properties); } /** * Get the fully qualified name of the calling logger sub-class/wrapper. * Provided for compatibility with log4j 1.3 * @return fully qualified class name, may be null. * @since 1.2.15 */ public String getFQNOfLoggerClass() { return fqnOfCategoryClass; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -