Skip to content

Commit

Permalink
ZCS-11112 : Worked on review comments, removed fully qualified names …
Browse files Browse the repository at this point in the history
…and commented classes
  • Loading branch information
abhishekdeshmukh2209 committed May 18, 2022
1 parent 27e5aa6 commit 01832b4
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 160 deletions.
19 changes: 10 additions & 9 deletions common/src/java/com/zimbra/common/util/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.common.collect.ImmutableMap;

/**
Expand All @@ -35,7 +36,7 @@
*/
public class Log {

private final Map<String, org.apache.logging.log4j.Logger> mAccountLoggers = new ConcurrentHashMap<String, org.apache.logging.log4j.Logger>();
private final Map<String, Logger> mAccountLoggers = new ConcurrentHashMap<String, Logger>();

private static final Map<Level, org.apache.logging.log4j.Level> ZIMBRA_TO_LOG4J =
new EnumMap<Level, org.apache.logging.log4j.Level>(Level.class);
Expand All @@ -61,9 +62,9 @@ public enum Level {
error, warn, info, debug, trace;
};

private final org.apache.logging.log4j.Logger mLogger;
private final Logger mLogger;

Log(org.apache.logging.log4j.Logger logger) {
Log(Logger logger) {
if (logger == null) {
throw new IllegalStateException("logger cannot be null");
}
Expand All @@ -83,7 +84,7 @@ public void addAccountLogger(String accountName, Level level) {
}

// Create the account logger if it doesn't already exist.
org.apache.logging.log4j.Logger accountLogger = mAccountLoggers.get(accountName);
Logger accountLogger = mAccountLoggers.get(accountName);
if (accountLogger == null) {
String accountCategory = getAccountCategory(getCategory(), accountName);
accountLogger = LogManager.getLogger(accountCategory);
Expand All @@ -107,7 +108,7 @@ public int removeAccountLoggers() {
* @return <tt>true</tt> if the logger was removed
*/
public boolean removeAccountLogger(String accountName) {
org.apache.logging.log4j.Logger logger = mAccountLoggers.remove(accountName);
Logger logger = mAccountLoggers.remove(accountName);
return (logger != null);
}

Expand Down Expand Up @@ -564,7 +565,7 @@ List<AccountLogger> getAccountLoggers() {
}
List<AccountLogger> accountLoggers = new ArrayList<AccountLogger>();
for (String accountName : mAccountLoggers.keySet()) {
org.apache.logging.log4j.Logger log4jLogger = mAccountLoggers.get(accountName);
Logger log4jLogger = mAccountLoggers.get(accountName);
AccountLogger al = new AccountLogger(mLogger.getName(), accountName,
LOG4J_TO_ZIMBRA.get(log4jLogger.getLevel()));
accountLoggers.add(al);
Expand All @@ -579,12 +580,12 @@ List<AccountLogger> getAccountLoggers() {
*
* @see #addAccountLogger
*/
private org.apache.logging.log4j.Logger getLogger() {
private Logger getLogger() {
if (mAccountLoggers.size() == 0) {
return mLogger;
}
for (String accountName : ZimbraLog.getAccountNamesFromContext()) {
org.apache.logging.log4j.Logger logger = mAccountLoggers.get(accountName);
Logger logger = mAccountLoggers.get(accountName);
if (logger != null) {
return logger;
}
Expand Down
12 changes: 4 additions & 8 deletions common/src/java/com/zimbra/common/util/LogFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ private LogFactory() {

public synchronized static void init() {
try {
ConfigurationSource logConfigSource = new ConfigurationSource(new FileInputStream(LC.zimbra_log4j_properties.value()));
Configurator.initialize(null, logConfigSource);
} catch (IOException e) {
Configurator.initialize(null, LC.zimbra_log4j_properties.value());
} catch (Exception e) {
ZimbraLog.misc.info("Error initializing the loggers.", e);
}
}
Expand All @@ -59,11 +58,9 @@ public synchronized static void reset() {
log.removeAccountLoggers();
}
LogManager.shutdown();
ConfigurationSource logConfigSource;
try {
logConfigSource = new ConfigurationSource(new FileInputStream(LC.zimbra_log4j_properties.value()));
Configurator.initialize(null, logConfigSource);
} catch (IOException e) {
Configurator.initialize(null, LC.zimbra_log4j_properties.value());
} catch (Exception e) {
ZimbraLog.misc.info("Error resetting the loggers.", e);
}
}
Expand Down Expand Up @@ -92,7 +89,6 @@ public static Log getLog(String name) {
*/
public static boolean logExists(String name) {
return (LogManager.exists(name));

}

/**
Expand Down
27 changes: 0 additions & 27 deletions common/src/java/com/zimbra/common/util/ZimbraPattern2Layout.java

This file was deleted.

73 changes: 26 additions & 47 deletions common/src/java/com/zimbra/common/util/ZimbraPatternLayout.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
/*
* ***** BEGIN LICENSE BLOCK ***** Zimbra Collaboration Suite Server Copyright
* (C) 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. *****
* END LICENSE BLOCK *****
*
* package com.zimbra.common.util;
*
* import org.apache.log4j.Category; import org.apache.log4j.ConsoleAppender;
* import org.apache.log4j.Layout; import org.apache.log4j.PatternLayout; import
* org.apache.log4j.helpers.PatternParser;
*
*//**
* Subclasses Log4J's <tt>PatternLayout</tt> class to add additional support for
* the <tt>%z</tt> option, which prints the value returned by
* {@link ZimbraLog#getContextString()}.
*
* @author bburtin
*//*
* public class ZimbraPatternLayout extends PatternLayout {
*
* public ZimbraPatternLayout() { this(DEFAULT_CONVERSION_PATTERN); }
*
* public ZimbraPatternLayout(String pattern) { super(pattern); }
*
* public PatternParser createPatternParser(String pattern) { if (pattern ==
* null) { pattern = DEFAULT_CONVERSION_PATTERN; } return new
* ZimbraPatternParser(pattern, this); }
*
* public static void main(String[] args) { Layout layout = new
* ZimbraPatternLayout("[%z] - %m%n"); Category cat =
* Category.getInstance("some.cat"); cat.addAppender(new ConsoleAppender(layout,
* ConsoleAppender.SYSTEM_OUT));
* ZimbraLog.addAccountNameToContext("[email protected]");
* ZimbraLog.addMboxToContext(99); cat.debug("Hello, log");
* cat.info("Hello again..."); ZimbraLog.clearContext();
* cat.info("No more context"); } }
*/
package com.zimbra.common.util;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;

@Plugin(name = "ZimbraPatternLayout", category = "Converter")
@ConverterKeys({ "z" })
public class ZimbraPatternLayout extends LogEventPatternConverter {

protected ZimbraPatternLayout(String name, String style) {
super(name, style);
}

public static ZimbraPatternLayout newInstance(String[] options)
{
return new ZimbraPatternLayout("z", Thread.currentThread().getName());
}

@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
toAppendTo.append(ZimbraLog.getContextString() == null ? "" : ZimbraLog.getContextString());
}

}
46 changes: 0 additions & 46 deletions common/src/java/com/zimbra/common/util/ZimbraPatternParser.java

This file was deleted.

3 changes: 2 additions & 1 deletion soap/src/java/com/zimbra/soap/util/XsdCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -59,7 +60,7 @@
* See http://connect.microsoft.com/VisualStudio/feedback/details/471297
*/
public class XsdCleaner {
private static final org.apache.logging.log4j.Logger LOG = LogManager.getLogger(XsdCleaner.class);
private static final Logger LOG = LogManager.getLogger(XsdCleaner.class);

private static final String ARG_DIR = "--dir";
private static final String KEEP_BACKUPS = "--keep.backups";
Expand Down
6 changes: 1 addition & 5 deletions store/src/java/com/zimbra/cs/imap/ImapDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ private void stopServers() {

public static void main(String[] args) {
try {
Properties props = new Properties();
try (FileInputStream fisLog4j = new FileInputStream(IMAPD_LOG4J_CONFIG)) {
props.load(fisLog4j);
}
try {
ConfigurationSource logConfigSource = new ConfigurationSource(new FileInputStream(LC.zimbra_log4j_properties.value()));
ConfigurationSource logConfigSource = new ConfigurationSource(new FileInputStream(IMAPD_LOG4J_CONFIG));
Configurator.initialize(null, logConfigSource);
} catch (IOException e) {
ZimbraLog.misc.info("Error initializing the loggers.", e);
Expand Down
36 changes: 19 additions & 17 deletions store/src/java/com/zimbra/cs/index/IndexEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -34,11 +35,14 @@
import java.util.Set;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.OutputStreamAppender;
import org.apache.logging.log4j.core.appender.WriterAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
Expand Down Expand Up @@ -435,22 +439,20 @@ protected boolean authenticate() throws IOException {
return true;
}

private org.apache.logging.log4j.core.appender.WriterAppender mAppender;
private WriterAppender mAppender;

public boolean enableLogging() {
if (mAppender == null) {
/*
* Layout layout = new PatternLayout(logLayoutPattern );
* mAppender = new org.apache.logging.log4j.core.appender.WriterAppender(layout, mOutputStream);
* Logger root = Logger.getRootLogger();
* root.addAppender(mAppender);
*/
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.newBuilder().withPattern(logLayoutPattern).build();
final Appender appender = OutputStreamAppender.createAppender(layout, null, mOutputStream, mAppender.getName(), false, true);
appender.start();
config.addAppender(appender);
Layout layout = PatternLayout.newBuilder().withPattern(logLayoutPattern).build();
mAppender = WriterAppender.newBuilder().setTarget(new OutputStreamWriter(mOutputStream))
.setLayout(layout).build();

Logger root = LogManager.getRootLogger();
LoggerContext context = LoggerContext.getContext(false);
Configuration configuration = context.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(root.getName());

loggerConfig.addAppender(mAppender, Level.INFO, null);
return true;
} else {
return false;
Expand Down Expand Up @@ -714,7 +716,7 @@ public void logLevel()
return;
}

root.setLevel(newLevel);
Configurator.setLevel(root.getName(), newLevel);
}
Level cur = root.getLevel();
outputStream.println("Current level is: "+cur);
Expand Down

0 comments on commit 01832b4

Please sign in to comment.