diff --git a/tajo-common/src/main/java/org/apache/tajo/logging/JULDefaultConfiguration.java b/tajo-common/src/main/java/org/apache/tajo/logging/JULDefaultConfiguration.java new file mode 100644 index 0000000000..553a1a36e2 --- /dev/null +++ b/tajo-common/src/main/java/org/apache/tajo/logging/JULDefaultConfiguration.java @@ -0,0 +1,49 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.logging; + +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.LogManager; + +/** + * Initiate JUL handlers by using pre-defined default properties. + */ +public class JULDefaultConfiguration { + + private static final String defaultLoggingPropertiesName = "jul-logging.properties"; + + public JULDefaultConfiguration() throws SecurityException, IOException { + InputStream configFileInputStream = null; + + try { + configFileInputStream = ClassLoader.getSystemClassLoader() + .getResourceAsStream(defaultLoggingPropertiesName); + LogManager.getLogManager().readConfiguration(configFileInputStream); + } catch (SecurityException e) { + throw e; + } catch (IOException e) { + throw e; + } finally { + if (configFileInputStream != null) { + configFileInputStream.close(); + } + } + } +} diff --git a/tajo-common/src/main/java/org/apache/tajo/logging/Log4JHandler.java b/tajo-common/src/main/java/org/apache/tajo/logging/Log4JHandler.java new file mode 100644 index 0000000000..54a00c640b --- /dev/null +++ b/tajo-common/src/main/java/org/apache/tajo/logging/Log4JHandler.java @@ -0,0 +1,119 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.logging; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; +import java.util.Properties; +import java.util.logging.ErrorManager; +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +import org.apache.log4j.Category; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.log4j.spi.LocationInfo; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.spi.ThrowableInformation; + +/** + * Simple handlers for converting JUL logging events to log4j logging events. + */ +public class Log4JHandler extends Handler { + + private static final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + private static final Properties emptyProperties = new Properties(); + + public Log4JHandler() { + } + + private String getThreadName(long threadId) { + ThreadInfo threadInfo = null; + String threadName; + + try { + threadInfo = threadMXBean.getThreadInfo(threadId); + threadName = threadInfo.getThreadName(); + } catch (Throwable e) { + threadName = "thread-" + threadId; + } + + return threadName; + } + + private LoggingEvent convertLoggingEvent(LogRecord logRecord) { + String loggerName = logRecord.getLoggerName(); + Logger logger = LogManager.getLogger(logRecord.getLoggerName()); + ThrowableInformation throwableInformation = null; + LocationInfo locationInfo = null; + String localizedMessage = null; + + if (logRecord.getThrown() != null) { + throwableInformation = new ThrowableInformation(logRecord.getThrown()); + } + + try { + if (getFormatter() != null) { + localizedMessage = getFormatter().format(logRecord); + } else { + localizedMessage = logRecord.getMessage(); + } + } catch (Exception e) { + reportError(null, e, ErrorManager.FORMAT_FAILURE); + } + + locationInfo = new LocationInfo(null, + logRecord.getSourceClassName(), + logRecord.getSourceMethodName(), + null); + + return new LoggingEvent(loggerName, + logger, + logRecord.getMillis(), + Log4JLevelConverter.getLog4JLevel(logRecord.getLevel()), + localizedMessage, + getThreadName(logRecord.getThreadID()), + throwableInformation, + null, + locationInfo, + emptyProperties); + } + + @Override + public void publish(LogRecord record) { + LoggingEvent loggingEvent = convertLoggingEvent(record); + Category logger = loggingEvent.getLogger(); + + if (loggingEvent.getLevel().isGreaterOrEqual(logger.getEffectiveLevel())) { + logger.callAppenders(loggingEvent); + } + } + + @Override + public void flush() { + + } + + @Override + public void close() throws SecurityException { + + } + +} diff --git a/tajo-common/src/main/java/org/apache/tajo/logging/Log4JLevelConverter.java b/tajo-common/src/main/java/org/apache/tajo/logging/Log4JLevelConverter.java new file mode 100644 index 0000000000..1da83857e6 --- /dev/null +++ b/tajo-common/src/main/java/org/apache/tajo/logging/Log4JLevelConverter.java @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.logging; + +import java.util.HashMap; +import java.util.Map; + +/** + * Description on level conversions can be found in this following link.
+ * Description for level conversions + */ +public class Log4JLevelConverter { + + private static final Map JULtoLog4j = + new HashMap(10); + + static { + JULtoLog4j.put(java.util.logging.Level.OFF, org.apache.log4j.Level.OFF); + JULtoLog4j.put(java.util.logging.Level.SEVERE, org.apache.log4j.Level.FATAL); + JULtoLog4j.put(java.util.logging.Level.WARNING, org.apache.log4j.Level.WARN); + JULtoLog4j.put(java.util.logging.Level.INFO, org.apache.log4j.Level.INFO); + JULtoLog4j.put(java.util.logging.Level.CONFIG, org.apache.log4j.Level.DEBUG); + JULtoLog4j.put(java.util.logging.Level.FINE, org.apache.log4j.Level.DEBUG); + JULtoLog4j.put(java.util.logging.Level.FINER, org.apache.log4j.Level.TRACE); + JULtoLog4j.put(java.util.logging.Level.FINEST, org.apache.log4j.Level.TRACE); + JULtoLog4j.put(java.util.logging.Level.ALL, org.apache.log4j.Level.ALL); + } + + public static org.apache.log4j.Level getLog4JLevel(java.util.logging.Level julLevel) { + return JULtoLog4j.get(julLevel); + } +} diff --git a/tajo-common/src/main/resources/jul-logging.properties b/tajo-common/src/main/resources/jul-logging.properties new file mode 100644 index 0000000000..05f971a63c --- /dev/null +++ b/tajo-common/src/main/resources/jul-logging.properties @@ -0,0 +1,23 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +handlers= org.apache.tajo.logging.Log4JHandler +.level= INFO + +org.apache.tajo.logging.Log4JHandler.formatter= java.util.logging.SimpleFormatter +java.util.logging.SimpleFormatter.format= "%1$tc %2$s%n%4$s: %5$s%6$s%n" \ No newline at end of file diff --git a/tajo-core/src/test/java/org/apache/tajo/TajoTestingCluster.java b/tajo-core/src/test/java/org/apache/tajo/TajoTestingCluster.java index f7fb2f2186..cef50c89f1 100644 --- a/tajo-core/src/test/java/org/apache/tajo/TajoTestingCluster.java +++ b/tajo-core/src/test/java/org/apache/tajo/TajoTestingCluster.java @@ -22,6 +22,7 @@ import com.google.common.base.Preconditions; import com.google.common.io.Closeables; import com.google.common.io.Files; + import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -40,6 +41,7 @@ import org.apache.tajo.conf.TajoConf; import org.apache.tajo.conf.TajoConf.ConfVars; import org.apache.tajo.engine.planner.global.rewriter.GlobalPlanTestRuleProvider; +import org.apache.tajo.logging.JULDefaultConfiguration; import org.apache.tajo.master.TajoMaster; import org.apache.tajo.master.rm.TajoWorkerResourceManager; import org.apache.tajo.plan.rewrite.LogicalPlanTestRuleProvider; @@ -161,6 +163,14 @@ void initPropertiesAndConfigs() { conf.setIntVar(ConfVars.WORKER_HISTORY_EXPIRE_PERIOD, 1); /* Since Travi CI limits the size of standard output log up to 4MB */ + System.setProperty("java.util.logging.config.class", JULDefaultConfiguration.class.getName()); + try { + java.util.logging.LogManager.getLogManager().readConfiguration(); + } catch (Exception ignored) { + if (LOG.isDebugEnabled()) { + LOG.debug(ignored.getMessage(), ignored); + } + } if (!StringUtils.isEmpty(LOG_LEVEL)) { Level defaultLevel = Logger.getRootLogger().getLevel(); Logger.getLogger("org.apache.tajo").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel)); @@ -168,6 +178,7 @@ void initPropertiesAndConfigs() { Logger.getLogger("org.apache.zookeeper").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel)); Logger.getLogger("BlockStateChange").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel)); Logger.getLogger("org.mortbay.log").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel)); + Logger.getLogger("org.glassfish.jersey").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel)); } } diff --git a/tajo-dist/src/main/bin/tajo b/tajo-dist/src/main/bin/tajo index a92d1a9293..cca85e280f 100755 --- a/tajo-dist/src/main/bin/tajo +++ b/tajo-dist/src/main/bin/tajo @@ -384,6 +384,7 @@ if [ "$COMMAND" = "classpath" ] ; then elif [ "$COMMAND" = "master" ] ; then CLASS='org.apache.tajo.master.TajoMaster' TAJO_OPTS="$TAJO_OPTS $JAVA_TAJO_MASTER_HEAP_MAX $TAJO_MASTER_OPTS" + TAJO_OPTS="$TAJO_OPTS -Djava.util.logging.config.class=org.apache.tajo.logging.JULDefaultConfiguration" elif [ "$COMMAND" = "worker" ] ; then CLASS='org.apache.tajo.worker.TajoWorker' TAJO_OPTS="$TAJO_OPTS $JAVA_WORKER_HEAP_MAX $TAJO_WORKER_OPTS"