通常我们在开发应用的时候需要将日志打印到终端,而当应用发布的时候就需要将日志记录到本地文件,因此在具体实现的时候使用了简单工厂模式。本地日志的实现已经有很多现成的优秀开源库,这里就不再重复造轮子了,本文使用的是Apache的开源库log4j,版本号1.2.17。下面来介绍具体实现。
先来预览一下文件目录结构:
![5462305-b52c6a5d4edd50d0.png](https://i-blog.csdnimg.cn/blog_migrate/85c6785c48e6cd9a1391c0c160e81013.webp?x-image-process=image/format,png)
ILog.java文件
package xxx.log;
public interface ILog {
int LEVEL_TRACE = 0;
int LEVEL_DEBUG = 1;
int LEVEL_INFO = 2;
int LEVEL_WARN = 3;
int LEVEL_ERROR = 4;
int LEVEL_FATAL = 5;
void t(Object msg);
void d(Object msg);
void i(Object msg);
void w(Object msg);
void e(Object msg);
}
LogPrintImpl.java
package xxx.log;
import android.util.Log;
public class LogPrintImpl implements ILog {
private final String mTag;
private final int mLevel;
public LogPrintImpl(int level, String tag) {
mTag = tag;
mLevel = level;
}
@Override
public void t(Object msg) {
if (mLevel > LEVEL_TRACE) {
return;
}
if (!(msg instanceof String)) {
msg = msg.toString();
}
Log.d(mTag, (String)msg);
}
@Override
public void d(Object msg) {
if (mLevel > LEVEL_DEBUG) {
return;
}
if (!(msg instanceof String)) {
msg = msg.toString();
}
Log.d(mTag, (String)msg);
}
@Override
public void i(Object msg) {
if (mLevel > LEVEL_INFO) {
return;
}
if (!(msg instanceof String)) {
msg = msg.toString();
}
Log.i(mTag, (String)msg);
}
@Override
public void w(Object msg) {
if (mLevel > LEVEL_WARN) {
return;
}
if (!(msg instanceof String)) {
msg = msg.toString();
}
Log.w(mTag, (String)msg);
}
@Override
public void e(Object msg) {
if (mLevel > LEVEL_ERROR) {
return;
}
if (!(msg instanceof String)) {
msg = msg.toString();
}
Log.e(mTag, (String)msg);
}
}
LogFileImpl.java
package xxx.log;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import java.io.IOException;
public class LogFileImpl implements ILog {
//constant
private static final int MAX_FILE_SIZE = 10 * 1024 * 1024;//10M bytes
//non-constant
private final String mTag;
private org.apache.log4j.Logger mLogger;
public LogFileImpl(int level, String tag, String path) {
mTag = tag;
init(path, level);
}
public void init(String path, int level) {
PatternLayout patternLayout = new PatternLayout();
patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} -%-5.5p- %m %n");
RollingFileAppender rollingFileAppender = null;
try {
rollingFileAppender = new RollingFileAppender(patternLayout, path, true);
rollingFileAppender.setMaximumFileSize(MAX_FILE_SIZE);
rollingFileAppender.setMaxBackupIndex(2);
} catch (IOException ex) {
}
mLogger = org.apache.log4j.Logger.getLogger(mTag);
mLogger.addAppender(rollingFileAppender);
Level l;
switch(level) {
case LEVEL_TRACE:
l = Level.TRACE;
break;
case LEVEL_DEBUG:
l = Level.DEBUG;
break;
case LEVEL_INFO:
l = Level.INFO;
break;
case LEVEL_ERROR:
l = Level.ERROR;
break;
case LEVEL_FATAL:
l = Level.FATAL;
break;
default:
l = Level.ERROR;
break;
}
mLogger.setLevel(l);
}
@Override
public void t(Object msg) {
if (mLogger != null) {
mLogger.trace(msg);
}
}
@Override
public void d(Object msg) {
if (mLogger != null) {
mLogger.debug(msg);
}
}
@Override
public void i(Object msg) {
if (mLogger != null) {
mLogger.info(msg);
}
}
@Override
public void w(Object msg) {
if (mLogger != null) {
mLogger.warn(msg);
}
}
@Override
public void e(Object msg) {
if (mLogger != null) {
mLogger.error(msg);
}
}
}
LogFactory.java
package xxx.log;
public class LogFactory {
public ILog createLogger(int type, int level, String tag, String path) {
ILog instance = null;
switch(type) {
case 0:
instance = new LogPrintImpl(level, tag);
break;
case 1:
instance = new LogFileImpl(level, tag, path);
break;
default:
break;
}
return instance;
}
}
LogUtil.java
package xxx.log;
public class LogUtils {
private static ILog mLogger;
public static void init(int type, int level, String tag, String path) {
if (mLogger == null) {
synchronized (LogUtils.class) {
if (mLogger == null) {
LogFactory logFactory = new LogFactory();
mLogger = logFactory.createLogger(type, level, tag, path);
}
}
}
}
public static void t(Object msg) {
if (mLogger != null) {
mLogger.t(msg);
}
}
public static void d(Object msg) {
if (mLogger != null) {
mLogger.d(msg);
}
}
public static void i(Object msg) {
if (mLogger != null) {
mLogger.i(msg);
}
}
public static void w(Object msg) {
if (mLogger != null) {
mLogger.w(msg);
}
}
public static void e(Object msg) {
if (mLogger != null) {
mLogger.e(msg);
}
}
}
使用日志类
LogUtils.init(0, ILog.LEVEL_INFO, "TAG", "");
//LogUtils.init(1, ILog.LEVEL_INFO, "TAG", "/sdcard/log");
LogUtils.i("Hello world!");