Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
119 changes: 119 additions & 0 deletions tajo-common/src/main/java/org/apache/tajo/logging/Log4JHandler.java
Original file line number Diff line number Diff line change
@@ -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 {

}

}
Original file line number Diff line number Diff line change
@@ -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. <br/>
* <a href="http://logging.apache.org/log4j/2.x/log4j-jul/index.html">Description for level conversions</a>
*/
public class Log4JLevelConverter {

private static final Map<java.util.logging.Level, org.apache.log4j.Level> JULtoLog4j =
new HashMap<java.util.logging.Level, org.apache.log4j.Level>(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);
}
}
23 changes: 23 additions & 0 deletions tajo-common/src/main/resources/jul-logging.properties
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions tajo-core/src/test/java/org/apache/tajo/TajoTestingCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -161,13 +163,22 @@ 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));
Logger.getLogger("org.apache.hadoop").setLevel(Level.toLevel(LOG_LEVEL.toUpperCase(), defaultLevel));
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));
}
}

Expand Down
1 change: 1 addition & 0 deletions tajo-dist/src/main/bin/tajo
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down