log

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

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

package cn.lt.android.util;

import android.text.TextUtils;

import android.util.Log;

import java.util.List;

import cn.lt.android.GlobalConfig;

import cn.lt.framework.util.StringUtils;

/**

* @author chengyong

* @time 2015-6-23 上午11:33:49

* @des 日志级别是LEVEL_ALL显示所有信息, 包括System.out.println信息

* @des 日志级别是LEVEL_OFF关闭所有信息, 包括System.out.println信息

*/

public class LogUtils {

    /**

    * 日志输出时的TAG

    */

    private static String mTag = "LT_AppCenter";

    /**

    * 日志输出级别NONE

    */

    public static final int LEVEL_OFF = 0;

    /**

    * 日志输出级别 ALL

    */

    public static final int LEVEL_ALL = 7;

    /**

    * 日志输出级别V

    */

    public static final int LEVEL_VERBOSE = 1;

    /**

    * 日志输出级别D

    */

    public static final int LEVEL_DEBUG = 2;

    /**

    * 日志输出级别I

    */

    public static final int LEVEL_INFO = 3;

    /**

    * 日志输出级别W

    */

    public static final int LEVEL_WARN = 4;

    /**

    * 日志输出级别E

    */

    public static final int LEVEL_ERROR = 5;

    /**

    * 日志输出级别S,自定义定义的一个级别

    */

    public static final int LEVEL_SYSTEM = 6;

    /**

    * 是否允许输出log

    */

    public static int mDebuggable = GlobalConfig.DEBUG ? LEVEL_ALL : LEVEL_OFF; //线上环境改为LEVEL_OFF

    /**

    * 用于记时的变量

    */

    private static long mTimestamp = 0;

    /**

    * 写文件的锁对象

    */

    private static final Object mLogLock = new Object();

    /**---------------日志输出,已固定TAG  begin---------------**/

    /**

    * 以级别为 d 的形式输出LOG

    */

    public static void v(String msg) {

        if (mDebuggable >= LEVEL_VERBOSE) {

            Log.v(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 d 的形式输出LOG

    */

    public static void d(String msg) {

        if (mDebuggable >= LEVEL_DEBUG) {

            Log.d(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 i 的形式输出LOG

    */

    public static void i(String msg) {

        if (mDebuggable >= LEVEL_INFO) {

            Log.i(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 w 的形式输出LOG

    */

    public static void w(String msg) {

        if (mDebuggable >= LEVEL_WARN) {

            Log.w(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 w 的形式输出Throwable

    */

    public static void w(Throwable tr) {

        if (mDebuggable >= LEVEL_WARN) {

            Log.w(mTag, "", tr);

        }

    }

    /**

    * 以级别为 w 的形式输出LOG信息和Throwable

    */

    public static void w(String msg, Throwable tr) {

        if (mDebuggable >= LEVEL_WARN && null != msg) {

            Log.w(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)), tr);

        }

    }

    /**

    * 以级别为 e 的形式输出LOG

    */

    public static void e(String msg) {

        if (mDebuggable >= LEVEL_ERROR) {

            Log.e(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 s 的形式输出LOG,主要是为了System.out.println,稍微格式化了一下

    */

    public static void sf(String msg) {

        if (mDebuggable >= LEVEL_ERROR) {

            System.out.println("----------" + msg + "----------");

        }

    }

    /**

    * 以级别为 s 的形式输出LOG,主要是为了System.out.println

    */

    public static void s(String msg) {

        if (mDebuggable >= LEVEL_ERROR) {

            System.out.println(JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 e 的形式输出Throwable

    */

    public static void e(Throwable tr) {

        if (mDebuggable >= LEVEL_ERROR) {

            Log.e(mTag, "", tr);

        }

    }

    /**

    * 以级别为 e 的形式输出LOG信息和Throwable

    */

    public static void e(String msg, Throwable tr) {

        if (mDebuggable >= LEVEL_ERROR && null != msg) {

            Log.e(mTag, JsonFormat.format(StringUtils.decodeUnicode(msg)), tr);

        }

    }

    /**---------------日志输出,已固定TAG  end---------------**/

    /**---------------日志输出,未固定TAG  begin---------------**/

    /**

    * 以级别为 d 的形式输出LOG

    */

    public static void v(String tag, String msg) {

        if (mDebuggable >= LEVEL_VERBOSE) {

            Log.v(tag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 d 的形式输出LOG

    */

    public static void d(String tag, String msg) {

        if (mDebuggable >= LEVEL_DEBUG) {

            Log.d(tag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 i 的形式输出LOG

    */

    public static void i(String tag, String msg) {

        if (mDebuggable >= LEVEL_INFO) {

            Log.i(tag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 w 的形式输出LOG

    */

    public static void w(String tag, String msg) {

        if (mDebuggable >= LEVEL_WARN) {

            Log.w(tag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**

    * 以级别为 e 的形式输出LOG

    */

    public static void e(String tag, String msg) {

        if (mDebuggable >= LEVEL_ERROR) {

            Log.e(tag, JsonFormat.format(StringUtils.decodeUnicode(msg)));

        }

    }

    /**---------------日志输出,未固定TAG  end---------------**/

    /**

    * 把Log存储到文件中

    *

    * @param log  需要存储的日志

    * @param path 存储路径

    */

    public static void log2File(String log, String path) {

        log2File(log, path, true);

    }

    public static void log2File(String log, String path, boolean append) {

        synchronized (mLogLock) {

//            FileUtils.writeFile(log + "\r\n", path, append);

        }

    }

    /**

    * 以级别为 e 的形式输出msg信息,附带时间戳,用于输出一个时间段起始点

    *

    * @param msg 需要输出的msg

    */

    public static void msgStartTime(String msg) {

        mTimestamp = System.currentTimeMillis();

        if (!TextUtils.isEmpty(msg)) {

            e("[Started:" + mTimestamp + "]" + msg);

        }

    }

    /**

    * 以级别为 e 的形式输出msg信息,附带时间戳,用于输出一个时间段结束点* @param msg 需要输出的msg

    */

    public static void elapsed(String msg) {

        long currentTime = System.currentTimeMillis();

        long elapsedTime = currentTime - mTimestamp;

        mTimestamp = currentTime;

        e("[Elapsed:" + elapsedTime + "]" + msg);

    }

    public static <T> void printList(List<T> list) {

        if (list == null || list.size() < 1) {

            return;

        }

        int size = list.size();

        i("---begin---");

        for (int i = 0; i < size; i++) {

            i(i + ":" + list.get(i).toString());

        }

        i("---end---");

    }

    public static <T> void printArray(T[] array) {

        if (array == null || array.length < 1) {

            return;

        }

        int length = array.length;

        i("---begin---");

        for (int i = 0; i < length; i++) {

            i(i + ":" + array[i].toString());

        }

        i("---end---");

    }

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容