📄 logger.java
字号:
* @param logTargets the Log Targets */ public synchronized void setLogTargets(final LogTarget[] logTargets) { m_logTargets = logTargets; setupErrorHandlers(); m_logTargetsForceSet = true; resetChildLogTargets(false); } /** * Unset the logtargets for this logger. * This logger (and thus all child loggers who don't specify logtargets) will * inherit from the parents LogTargets. */ public synchronized void unsetLogTargets() { unsetLogTargets(false); } /** * Unset the logtargets for this logger and all child loggers if recursive is set. * The loggers unset (and all child loggers who don't specify logtargets) will * inherit from the parents LogTargets. */ public synchronized void unsetLogTargets(final boolean recursive) { if (null != m_parent) m_logTargets = m_parent.safeGetLogTargets(); else m_logTargets = null; m_logTargetsForceSet = false; resetChildLogTargets(recursive); } /** * Get all the child Loggers of current logger. * * @return the child loggers */ public synchronized Logger[] getChildren() { if (null == m_children) return new Logger[0]; final Logger[] children = new Logger[m_children.length]; for (int i = 0; i < children.length; i++) { children[i] = m_children[i]; } return children; } /** * Create a new child logger. * The category of child logger is [current-category].subcategory * * @param subCategory the subcategory of this logger * @return the new logger * @throws IllegalArgumentException if subCategory has an empty element name */ public synchronized Logger getChildLogger(final String subCategory) throws IllegalArgumentException { final int end = subCategory.indexOf(CATEGORY_SEPARATOR); String nextCategory = null; String remainder = null; if (-1 == end) nextCategory = subCategory; else { if (end == 0) { throw new IllegalArgumentException("Logger categories MUST not have empty elements"); } nextCategory = subCategory.substring(0, end); remainder = subCategory.substring(end + 1); } //Get FQN for category String category = null; if (m_category.equals("")) category = nextCategory; else { category = m_category + CATEGORY_SEPARATOR + nextCategory; } //Check existing children to see if they //contain next Logger for step in category if (null != m_children) { for (int i = 0; i < m_children.length; i++) { if (m_children[i].m_category.equals(category)) { if (null == remainder) return m_children[i]; else return m_children[i].getChildLogger(remainder); } } } //Create new logger final Logger child = new Logger(m_errorHandler, category, null, this); //Add new logger to child list if (null == m_children) { m_children = new Logger[]{child}; } else { final Logger[] children = new Logger[m_children.length + 1]; System.arraycopy(m_children, 0, children, 0, m_children.length); children[m_children.length] = child; m_children = children; } if (null == remainder) return child; else return child.getChildLogger(remainder); } /** * Retrieve priority associated with Logger. * * @return the loggers priority * @deprecated This method violates Inversion of Control principle. * It will downgraded to protected access in a future * release. When user needs to check priority it is advised * that they use the is[Priority]Enabled() functions. */ public final Priority getPriority() { return m_priority; } /** * Retrieve category associated with logger. * * @return the Category * @deprecated This method violates Inversion of Control principle. * If you are relying on its presence then there may be * something wrong with the design of your system */ public final String getCategory() { return m_category; } /** * Get a copy of log targets for this logger. * * @return the child loggers */ public LogTarget[] getLogTargets() { // Jive change - we ignore the deprecated warning above and just return the log targets // since it's a closed system for us anyways return m_logTargets; } /** * Internal method to do actual outputting. * * @param priority the priority * @param message the message * @param throwable the throwable */ private final void output(final Priority priority, final String message, final Throwable throwable) { final LogEvent event = new LogEvent(); event.setCategory(m_category);// event.setContextStack( ContextStack.getCurrentContext( false ) ); event.setContextMap(ContextMap.getCurrentContext(false)); if (null != message) { event.setMessage(message); } else { event.setMessage(""); } event.setThrowable(throwable); event.setPriority(priority); //this next line can kill performance. It may be wise to //disable it sometimes and use a more granular approach event.setTime(System.currentTimeMillis()); output(event); } private final void output(final LogEvent event) { //cache a copy of targets for thread safety //It is now possible for another thread //to replace m_logTargets final LogTarget[] targets = m_logTargets; if (null == targets) { final String message = "LogTarget is null for category '" + m_category + "'"; m_errorHandler.error(message, null, event); } else if (!m_additivity) { fireEvent(event, targets); } else { //If log targets were not inherited, additivity is true //then fire an event to local targets if (m_logTargetsForceSet) { fireEvent(event, targets); } //if we have a parent Logger then send log event to parent if (null != m_parent) { m_parent.output(event); } } } private final void fireEvent(final LogEvent event, final LogTarget[] targets) { for (int i = 0; i < targets.length; i++) { //No need to clone array as addition of a log-target //will result in changin whole array targets[i].processEvent(event); } } /** * Update priority of children if any. */ private synchronized void resetChildPriorities(final boolean recursive) { if (null == m_children) return; final Logger[] children = m_children; for (int i = 0; i < children.length; i++) { children[i].resetPriority(recursive); } } /** * Update priority of this Logger. * If this loggers priority was manually set then ignore * otherwise get parents priority and update all children's priority. */ private synchronized void resetPriority(final boolean recursive) { if (recursive) { m_priorityForceSet = false; } else if (m_priorityForceSet) { return; } m_priority = m_parent.m_priority; resetChildPriorities(recursive); } /** * Retrieve logtarget array contained in logger. * This method is provided so that child Loggers can access a * copy of parents LogTargets. * * @return the array of LogTargets */ private synchronized LogTarget[] safeGetLogTargets() { if (null == m_logTargets) { if (null == m_parent) return new LogTarget[0]; else return m_parent.safeGetLogTargets(); } else { final LogTarget[] logTargets = new LogTarget[m_logTargets.length]; for (int i = 0; i < logTargets.length; i++) { logTargets[i] = m_logTargets[i]; } return logTargets; } } /** * Update logTargets of children if any. */ private synchronized void resetChildLogTargets(final boolean recursive) { if (null == m_children) return; for (int i = 0; i < m_children.length; i++) { m_children[i].resetLogTargets(recursive); } } /** * Set ErrorHandlers of LogTargets if necessary. */ private synchronized void setupErrorHandlers() { if (null == m_logTargets) return; for (int i = 0; i < m_logTargets.length; i++) { final LogTarget target = m_logTargets[i]; if (target instanceof ErrorAware) { ((ErrorAware)target).setErrorHandler(m_errorHandler); } } } /** * Update logTarget of this Logger. * If this loggers logTarget was manually set then ignore * otherwise get parents logTarget and update all children's logTarget. */ private synchronized void resetLogTargets(final boolean recursive) { if (recursive) { m_logTargetsForceSet = false; } else if (m_logTargetsForceSet) { return; } m_logTargets = m_parent.safeGetLogTargets(); resetChildLogTargets(recursive); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -