net.ymate.platform.log.AbstractLogger.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.log.AbstractLogger.java

Source

/*
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed 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 net.ymate.platform.log;

import net.ymate.platform.core.util.DateTimeUtils;
import org.apache.commons.lang.StringUtils;

/**
 * ?
 *
 * @author  (suninformation@163.com) on 2012-12-21 ?12:27:37
 * @version 1.0
 */
public abstract class AbstractLogger implements ILogger {

    private boolean __allowOutputConsole;

    // ?
    private int __depth = 3;

    // ????
    public static int PRINT_STACK_COUNT = 5;

    public ILogger console(boolean enable) {
        __allowOutputConsole = enable;
        return this;
    }

    public ILogger depth(int depth) {
        __depth = depth;
        return this;
    }

    protected abstract void __doLogWrite(LogLevel level, LogInfo content);

    protected void __doBuildEx(String info, Throwable e, ILogger.LogLevel level) {
        LogInfo _info = new LogInfo(getLoggerName(), level.getDispName(), Thread.currentThread().getId(),
                __doMakeCallerInfo(), info, __doMakeStackInfo(e),
                DateTimeUtils.formatTime(System.currentTimeMillis(), DateTimeUtils.YYYY_MM_DD_HH_MM_SS_SSS));
        //
        __doLogWrite(level, _info);
        // ??
        if (__allowOutputConsole) {
            System.out.println(_info.toString());
        }
    }

    /**
     * ??
     *
     * @return ??className.methodName:lineNumber?NO_STACK_TRACE:-1
     */
    protected String __doMakeCallerInfo() {
        StackTraceElement[] _stacks = new Throwable().getStackTrace();
        // ???
        if (__depth >= 0 && _stacks.length > 1 + __depth) {
            StackTraceElement _element = _stacks[1 + __depth];
            return StringUtils.substringBeforeLast(_element.getClassName(), ".").concat(".")
                    .concat(_element.getMethodName()).concat(":")
                    .concat(_element.getLineNumber() + StringUtils.EMPTY);
        }
        return "NO_STACK_TRACE:-1";
    }

    /**
     * ?
     *
     * @param e ?
     * @return ??
     */
    protected String __doMakeStackInfo(Throwable e) {
        if (e == null) {
            return StringUtils.EMPTY;
        }
        StringBuilder _stackSB = new StringBuilder(e.getClass().getName()).append(": ").append(StringUtils.EMPTY)
                .append(StringUtils.trimToEmpty(e.getMessage())).append("\n");
        StackTraceElement[] _stacks = e.getStackTrace();
        for (StackTraceElement _stack : _stacks) {
            _stackSB.append("\tat ") // 
                    .append(_stack).append("\n");
        }
        __ex(_stackSB, e.getCause());
        return _stackSB.toString();
    }

    /**
     * 
     *
     * @param stackSB ???
     * @param t       ?
     * @return ?true
     */
    protected boolean __ex(StringBuilder stackSB, Throwable t) {
        if (t != null) {
            stackSB.append("Caused by: ").append(t.getClass().getName()).append(": ")
                    .append(StringUtils.trimToEmpty(t.getMessage())).append("\n");
            StackTraceElement[] _traces = t.getStackTrace();
            int _tracesSize = _traces.length;
            for (int _idx = 0; _idx < _tracesSize; _idx++) {
                if (_idx < PRINT_STACK_COUNT) {
                    stackSB.append("\tat ") // 
                            .append(_traces[_idx]).append("\n");
                } else {
                    stackSB.append("\t... ").append(_tracesSize - PRINT_STACK_COUNT).append(" more\n");
                    break;
                }
            }
            if (__ex(stackSB, t.getCause())) {
                return true;
            }
        }
        return false;
    }
}