/**
 * The MIT License (MIT)
 * Copyright (c) 2009-2015 HONG LEIMING
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.yihu.hos.core.log.impl;

import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import org.apache.logging.log4j.Level;

public class Log4jLoggerFactory implements LoggerFactory.InternalLoggerFactory {

    public Logger getLogger(Class<?> clazz) {
        return new Log4jLogger(clazz);
    }

    public Logger getLogger(String name) {
        return new Log4jLogger(name);
    }
}

class Log4jLogger extends Logger {
    private static final String callerFQCN = Log4jLogger.class.getName();
    private org.apache.logging.log4j.Logger log;

    Log4jLogger(Class<?> clazz) {
        log = org.apache.logging.log4j.LogManager.getLogger(clazz);
    }

    Log4jLogger(String name) {
        log = org.apache.logging.log4j.LogManager.getLogger(name);
    }

    public void debug(String format, Object... args) {
        String msg = String.format(format, args);
        log.log(Level.DEBUG, callerFQCN, msg, null);
    }

    public void info(String format, Object... args) {
        String msg = String.format(format, args);
        log.log(Level.INFO, callerFQCN, msg, null);
    }

    public void warn(String format, Object... args) {
        String msg = String.format(format, args);
        log.log(Level.WARN, callerFQCN, msg, null);
    }

    public void error(String format, Object... args) {
        String msg = String.format(format, args);
        log.log(Level.ERROR, callerFQCN, msg, null);
    }


    public void info(String message) {
        log.log(Level.INFO, callerFQCN, message, null);
    }

    public void info(String message, Throwable t) {
        log.log(Level.INFO, callerFQCN, message, t);
    }

    public void debug(String message) {
        log.log(Level.DEBUG, callerFQCN, message, null);
    }

    public void debug(String message, Throwable t) {
        log.log(Level.DEBUG, callerFQCN, message, t);
    }

    public void warn(String message) {
        log.log(Level.WARN, callerFQCN, message, null);
    }

    public void warn(String message, Throwable t) {
        log.log(Level.WARN, callerFQCN, message, t);
    }

    public void error(String message) {
        log.log(Level.ERROR, callerFQCN, message, null);
    }

    public void error(String message, Throwable t) {
        log.log(Level.ERROR, callerFQCN, message, t);
    }

    public void fatal(String message) {
        log.log(Level.FATAL, callerFQCN, message, null);
    }

    public void fatal(String message, Throwable t) {
        log.log(Level.FATAL, callerFQCN, message, t);
    }

    public boolean isDebugEnabled() {
        return log.isDebugEnabled();
    }

    public boolean isInfoEnabled() {
        return log.isInfoEnabled();
    }

    public boolean isWarnEnabled() {
        return log.isWarnEnabled();
    }

    public boolean isErrorEnabled() {
        return log.isErrorEnabled();
    }

    public boolean isFatalEnabled() {
        return log.isFatalEnabled();
    }

    @Override
    public void trace(String message) {
        log.log(Level.TRACE, callerFQCN, message, null);
    }

    @Override
    public void trace(String message, Throwable t) {
        log.log(Level.TRACE, callerFQCN, message, t);
    }

    @Override
    public boolean isTraceEnabled() {
        return log.isTraceEnabled();
    }
}