Logger.java 7.56 KB
Newer Older
Kim Jinsung committed
1 2 3 4 5 6 7 8 9 10 11
package jp.odakyu.toiletsignage.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;

import jp.odakyu.toiletsignage.util.DateTimeFormat;
import jp.odakyu.toiletsignage.util.DateTimeUtil;
import jp.odakyu.toiletsignage.util.FileUtil;
12
import android.util.Log;
Kim Jinsung committed
13 14 15 16 17 18 19

public class Logger extends AbstractLogger {
	private static AbstractLogger logger = new Logger();
	private static PrintStream ps = System.out;
	private static String logPathFormat;
	private String currentDay;
	private int retentionPeriod = 1;
20
	private static boolean isLog = false;
Kim Jinsung committed
21 22 23 24 25 26 27 28 29 30
	
	public Logger() {
	}
	
	public static AbstractLogger getLogger() {
		return logger;
	}
	
	public void setLogPathFormat(String logPathFormat) {
		this.logPathFormat = logPathFormat;
31
		if (isLog) FileUtil.createParentDirectory(logPathFormat);
Kim Jinsung committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	}
	
	public static void setLogger(AbstractLogger abstractLogger) {
		logger = abstractLogger;
	}

	public void setRetentionPeriod(int retentionPeriod) {
		this.retentionPeriod = retentionPeriod;
	}

//	public static void v(String log) {
//		v(env.defaultLogName, log);
//	}

	public static void v(String tag, String log) {
		if (isVerboseEnabled()) {
			logger.verbose(tag, log);
		}
	}

	public static void v(String tag, String log, Object... param) {
		if (isVerboseEnabled()) {
			v(tag, String.format(log, param));
		}
	}

//	public static void v(String log, Throwable t) {
//		v(env.defaultLogName, log, t);
//	}
	
	public static void v(String tag, String log, Throwable t) {
		if (isVerboseEnabled()) {
			logger.verbose(tag, log, t);
		}
	}

//	public static void d(String log) {
//		d(env.defaultLogName, log);
//	}
	
	public static void d(String tag, String log) {
73
		Log.d(tag,log);
Kim Jinsung committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		if (isDebugEnabled()) {
			logger.debug(tag, log);
		}
	}
	
	public static void d(String tag, String log, Object... param) {
		if (isDebugEnabled()) {
			d(tag, String.format(log, param));
		}
	}
	
//	public static void d(String log, Throwable t) {
//		d(env.defaultLogName, log, t);
//	}
	
	public static void d(String tag, String log, Throwable t) {
90
		Log.d(tag,log, t);
Kim Jinsung committed
91 92 93 94 95 96 97 98 99 100
		if (isDebugEnabled()) {
			logger.debug(tag, log, t);
		}
	}
	
//	public static void i(String log) {
//		i(env.defaultLogName, log);
//	}
	
	public static void i(String tag, String log) {
101
		Log.i(tag,log);
Kim Jinsung committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		if (isInfoEnabled()) {
			logger.info(tag, log);
		}
	}
	
	public static void i(String tag, String log, Object... param) {
		if (isInfoEnabled()) {
			i(tag, String.format(log, param));
		}
	}
	
//	public static void i(String log, Throwable t) {
//		i(env.defaultLogName, log, t);
//	}
	
	public static void i(String tag, String log, Throwable t) {
118
		Log.i(tag,log, t);
Kim Jinsung committed
119 120 121 122 123 124 125 126 127 128
		if (isInfoEnabled()) {
			logger.info(tag, log, t);
		}
	}
	
//	public static void w(String log) {
//		w(env.defaultLogName, log);
//	}
	
	public static void w(String tag, String log) {
129
		Log.w(tag,log);
Kim Jinsung committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		if (isWarnEnabled()) {
			logger.warn(tag, log);
		}
	}
	
	public static void w(String tag, String log, Object... param) {
		if (isWarnEnabled()) {
			w(tag, String.format(log, param));
		}
	}
	
//	public static void w(String log, Throwable t) {
//		w(env.defaultLogName, log, t);
//	}
	
	public static void w(String tag, String log, Throwable t) {
146
		Log.w(tag,log);
Kim Jinsung committed
147 148 149 150 151 152 153 154 155 156
		if (isWarnEnabled()) {
			logger.warn(tag, log, t);
		}
	}
	
//	public static void e(String log) {
//		e(env.defaultLogName, log);
//	}
	
	public static void e(String tag, String log) {
157
		Log.e(tag,log);
Kim Jinsung committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		if (isErrorEnabled()) {
			logger.error(tag, log);
		}
	}
	
	public static void e(String tag, String log, Object... param) {
		if (isErrorEnabled()) {
			e(tag, String.format(log, param));
		}
	}
	
//	public static void e(String log, Throwable t) {
//		e(env.defaultLogName, log, t);
//	}
	
	public static void e(String tag, String log, Throwable t) {
174
		Log.e(tag,log, t);
Kim Jinsung committed
175 176 177 178 179 180 181 182 183
		if (isErrorEnabled()) {
			logger.error(tag, log, t);
		}
	}

    /**
     * #26344 端末ログファイル削除方式改善のため
     */
    private boolean isDeletable(File file) {
184

Kim Jinsung committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        long toTime = retentionPeriod  * 24 * 60 * 60 * 1000; // [retentionPeriod] 日
        long fromTime = (retentionPeriod + 7)  * 24 * 60 * 60 * 1000; // [retentionPeriod + 7] 日
        // fromTime < file.lastModified() < toTime
        if (file.lastModified() > System.currentTimeMillis() - fromTime && file.lastModified() < System.currentTimeMillis() -  toTime) {
            return true;
        }
        // ファイルのサイズが5Mを超えている場合
        if (file.lastModified() < System.currentTimeMillis() - fromTime && file.length() > 5 * 1024 * 1024) {
            return true;
        }
        return false;
    }

    private void log(String level, String tag, String log) {
        Date date = new Date();
        String day = DateTimeUtil.toString(date, DateTimeFormat.MMdd_none);
        if (logPathFormat != null) {
            if (currentDay == null || !currentDay.equals(day)) {
                File dir = new File(FileUtil.getParentPath(logPathFormat));
204 205 206 207 208 209 210 211 212 213 214 215
                //ログ削除処理しない
//                if (dir.exists()) {
//                    File[] files = dir.listFiles();
//                    if (files != null) {
//                        for (File file : files) { // 同一ディレクトリの旧ファイル(retentionPeriod日前)をすべて削除する
////                            if (file.lastModified() < System.currentTimeMillis() - retentionPeriod * 24 * 60 * 60 * 1000) {
//                            if (isDeletable(file)) {
//                                file.delete();
//                            }
//                        }
//                    }
//                }
Kim Jinsung committed
216 217 218 219 220 221 222 223 224 225
                currentDay = day;
                try {
                    ps = new PrintStream(new FileOutputStream(String.format(logPathFormat, day), true));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        String time = DateTimeUtil.toString(date, DateTimeFormat.MMddHHmmssSSS_hyphen);
226
		ps.println(time + ": [" + level + "] " + tag + "(" + Thread.currentThread().getId() + "): " + log);
Kim Jinsung committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    }

	private void log(String level, String defaultLogName, String log, Throwable e) {
		log(level, defaultLogName, log);
		e.printStackTrace(ps);
	}

	@Override
	protected void verbose(String tag, String log) {
		log("VERBOSE", tag, log);
	}

	@Override
	protected void verbose(String tag, String log, Throwable t) {
		log("VERBOSE", tag, log, t);
	}

	@Override
	protected void debug(String tag, String log) {
		log("DEBUG", tag, log);
	}

	@Override
	protected void debug(String tag, String log, Throwable t) {
		log("DEBUG", tag, log, t);
	}

	@Override
	protected void info(String tag, String log) {
		log("INFO", tag, log);
	}

	@Override
	protected void info(String tag, String log, Throwable t) {
		log("INFO", tag, log, t);
	}

	@Override
	protected void warn(String tag, String log) {
		log("WARN", tag, log);
	}

	@Override
	protected void warn(String tag, String log, Throwable t) {
		log("WARN", tag, log, t);
	}

	@Override
	protected void error(String tag, String log) {
		log("ERROR", tag, log);
	}

	@Override
	protected void error(String tag, String log, Throwable t) {
		log("ERROR", tag, log, t);
	}

	private static boolean isLoggable(int level) {
285
		return isLog;
Kim Jinsung committed
286 287 288
	}

	public static boolean isVerboseEnabled() {
289
		return isLog;
Kim Jinsung committed
290 291 292
	}
	
	public static boolean isDebugEnabled() {
293
    	return isLog;
Kim Jinsung committed
294 295 296
	}
	
	public static boolean isInfoEnabled() {
297
    	return isLog;
Kim Jinsung committed
298 299 300
	}
	
	public static boolean isWarnEnabled() {
301
		return isLog;
Kim Jinsung committed
302 303 304
	}
	
	public static boolean isErrorEnabled() {
305
    	return isLog;
Kim Jinsung committed
306 307 308
	}
	
}