Utility log tool : Log « Core Class « Android






Utility log tool

    
//package com.airplane.app.utils;

/*
 * Copyright (C) 2009 China Mobile Research Institute
 *
 * 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.
 */

import java.util.Hashtable;

import android.util.Config;
import android.util.Log;

/**
 * Utility log tool.
 *
 * @author MengZhao
 */
public class MyLogger {

    private static boolean sIsLoggerEnable = Config.DEBUG;

    private final static String LOG_TAG = "[SaveDataFlow]";

    private static Hashtable<String, MyLogger> sLoggerTable;

    static {
        sLoggerTable = new Hashtable<String, MyLogger>();

        if (Config.DEBUG)
            sIsLoggerEnable = true;
        else
            sIsLoggerEnable = false;
    }

    private String mClassName;

    public static MyLogger getLogger(String className) {
        MyLogger classLogger = (MyLogger) sLoggerTable.get(className);
        if (classLogger == null) {
            classLogger = new MyLogger(className);
            sLoggerTable.put(className, classLogger);
        }
        return classLogger;
    }

    private MyLogger(String name) {
        mClassName = name;
    }

    public void v(String log) {
        if (sIsLoggerEnable) {
            Log.v(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log);
        }
    }

    public void d(String log) {
        if (sIsLoggerEnable) {
            Log.d(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log);
        }
    }

    public void i(String log) {
        if (sIsLoggerEnable) {
            Log.i(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log);
        }
    }

    public void i(String log, Throwable tr) {
        if (sIsLoggerEnable) {
            Log.i(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log + "\n" + Log.getStackTraceString(tr));
        }
    }

    public void w(String log) {
        if (sIsLoggerEnable) {
            Log.w(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log);
        }
    }

    public void w(String log, Throwable tr) {
        if (sIsLoggerEnable) {
            Log.w(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log + "\n" + Log.getStackTraceString(tr));
        }
    }

    public void e(String log) {
        Log.e(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log);
    }

    public void e(String log, Throwable tr) {
        Log.e(LOG_TAG, "{Thread:" + Thread.currentThread().getName() + "}" + "[" + mClassName + ":] " + log + "\n" + Log.getStackTraceString(tr));
    }
}

   
    
    
    
  








Related examples in the same category

1.Use log
2.Log Utility
3.Log events
4.Log your action
5.Write an activity that looks like a pop-up dialog with a custom theme using a different text color.
6.Responsible for delegating calls to the Android logging system.
7.Dynamically defined), space efficient event logging to help instrument code for large scale stability and performance monitoring.
8.Write Exception Stack to Log
9.Logger and Logger Listener
10.Log Exception trace
11.Log a list of objects
12.Debug Util