PageRenderTime 20ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/logging/Log4jFactory.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 112 lines | 85 code | 12 blank | 15 comment | 51 complexity | ac7c7c0fc35c58675badc44c64623272 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.logging;
 18
 19import org.apache.log4j.Logger;
 20import org.apache.log4j.spi.LoggingEvent;
 21
 22import java.util.logging.Level;
 23import java.util.logging.LogRecord;
 24
 25public class Log4jFactory extends LoggerFactorySupport implements LoggerFactory {
 26
 27    protected ILogger createLogger(String name) {
 28        final Logger l = Logger.getLogger(name);
 29        return new Log4jLogger(l);
 30    }
 31
 32    class Log4jLogger implements ILogger {
 33        private final Logger logger;
 34        private final Level level;
 35
 36        public Log4jLogger(Logger logger) {
 37            this.logger = logger;
 38            if (logger.getLevel() == org.apache.log4j.Level.DEBUG) {
 39                level = Level.FINEST;
 40            } else if (logger.getLevel() == org.apache.log4j.Level.INFO) {
 41                level = Level.INFO;
 42            } else if (logger.getLevel() == org.apache.log4j.Level.WARN) {
 43                level = Level.WARNING;
 44            } else if (logger.getLevel() == org.apache.log4j.Level.FATAL) {
 45                level = Level.SEVERE;
 46            } else {
 47                level = Level.INFO;
 48            }
 49        }
 50
 51        public void log(Level level, String message) {
 52            if (Level.FINEST == level) {
 53                logger.debug(message);
 54            } else if (Level.SEVERE == level) {
 55                logger.fatal(message);
 56            } else if (Level.WARNING == level) {
 57                logger.warn(message);
 58            } else {
 59                logger.info(message);
 60            }
 61        }
 62
 63        public Level getLevel() {
 64            return level;
 65        }
 66
 67        public boolean isLoggable(Level level) {
 68            if (Level.FINEST == level) {
 69                return logger.isDebugEnabled();
 70            } else if (Level.WARNING == level) {
 71                return logger.isEnabledFor(org.apache.log4j.Level.WARN);
 72            } else if (Level.SEVERE == level) {
 73                return logger.isEnabledFor(org.apache.log4j.Level.FATAL);
 74            } else {
 75                return logger.isEnabledFor(org.apache.log4j.Level.INFO);
 76            }
 77        }
 78
 79        public void log(Level level, String message, Throwable thrown) {
 80            if (Level.FINEST == level) {
 81                logger.debug(message, thrown);
 82            } else if (Level.WARNING == level) {
 83                logger.warn(message, thrown);
 84            } else if (Level.SEVERE == level) {
 85                logger.fatal(message, thrown);
 86            } else {
 87                logger.info(message, thrown);
 88            }
 89        }
 90
 91        public void log(LogEvent logEvent) {
 92            LogRecord logRecord = logEvent.getLogRecord();
 93            String name = logEvent.getLogRecord().getLoggerName();
 94            org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(name);
 95            org.apache.log4j.Level level = null;
 96            if (logRecord.getLevel() == Level.FINEST) {
 97                level = org.apache.log4j.Level.DEBUG;
 98            } else if (logRecord.getLevel() == Level.INFO) {
 99                level = org.apache.log4j.Level.INFO;
100            } else if (logRecord.getLevel() == Level.WARNING) {
101                level = org.apache.log4j.Level.WARN;
102            } else if (logRecord.getLevel() == Level.SEVERE) {
103                level = org.apache.log4j.Level.FATAL;
104            } else {
105                level = org.apache.log4j.Level.INFO;
106            }
107            String message = logRecord.getMessage();
108            Throwable throwable = logRecord.getThrown();
109            logger.callAppenders(new LoggingEvent(name, logger, level, message, throwable));
110        }
111    }
112}