//************************************************************************
// author: yuzhengyang
// date: 2017.3.29 - 2018.4.27
// desc: 日志功能
// Copyright (c) yuzhengyang. All rights reserved.
//************************************************************************
//R.Log.IsWriteFile = true;
//R.Log.LogLevel = LogLevel.Warning | LogLevel.Debug;
//Log.AllocConsole();
//R.Log.v("this is v 啰嗦");
//R.Log.d("this is d 调试");
//R.Log.i("this is i 重要");
//R.Log.w("this is w 警告");
//R.Log.e("this is e 错误");
using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.DataUtils.StringUtils;
using Azylee.Core.IOUtils.DirUtils;
using Azylee.Core.IOUtils.FileUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.Core.WindowsUtils.ConsoleUtils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace Azylee.Core.LogUtils.SimpleLogUtils
{
///
/// Log 输出工具
///
/// 说明:
/// 1、Log.AllocConsole();开启控制台
/// 2、Log.FreeConsole();关闭控制台
/// 3、Log.i("information");输出消息
///
public class Log
{
#region 基础属性
//输出的 Log 格式
const string LOG_FORMAT = "{0} {1} {2}";
const string TIME_FORMAT = "HH:mm:ss.fff";
const string LOG_PATH = "azylee.log";
private int CACHE_DAYS = 30;//缓存天数
private object LogFileLock = new object();//写日志文件锁
private string LogPath = AppDomain.CurrentDomain.BaseDirectory + LOG_PATH;
private LogLevel ConsoleLogLevel = LogLevel.All;//日志输出到"控制台"等级
private LogLevel FileLogLevel = LogLevel.All;//日志输出到"文件"等级
#endregion
private Log() { }
///
/// 初始化 Log 工具
///
/// 已失效,使用WriteLevel代替
///
///
[Obsolete]
public Log(bool isWrite, LogLevel logLevel = LogLevel.All, LogLevel writeLevel = LogLevel.All)
{
if (isWrite)
{
//IsWriteFile = true;//已禁用,使用文件输出等级控制
ConsoleLogLevel = logLevel;
FileLogLevel = writeLevel;
}
}
///
/// 初始化 Log 工具
///
/// 控制台输出级别
/// 文件输出级别
public Log(LogLevel console = LogLevel.All, LogLevel file = LogLevel.All)
{
ConsoleLogLevel = console;
FileLogLevel = file;
}
///
/// 设置日志路径
///
///
public void SetLogPath(string path)
{
if (!string.IsNullOrWhiteSpace(path))
{
LogPath = DirTool.Combine(path, LOG_PATH);
}
}
///
/// 设置日志缓存天数(默认30天)
///
///
public void SetCacheDays(int days)
{
if (days >= 0) CACHE_DAYS = days;
}
#region Console 开启/关闭 API
///
/// 启用系统控制台输出
///
///
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
///
/// 关闭系统控制台
///
///
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
#endregion
///
/// 获取输出颜色
///
/// 输出类型
///
private ConsoleColor GetColor(LogType type)
{
switch (type)
{
case LogType.v: return ConsoleColor.Gray;
case LogType.d: return ConsoleColor.Blue;
case LogType.i: return ConsoleColor.Green;
case LogType.w: return ConsoleColor.Yellow;
case LogType.e: return ConsoleColor.Red;
default: return ConsoleColor.Gray;
}
}
///
/// 写出到控制台
///
/// 类型
/// 消息
private void WriteConsole(LogType type, string message)
{
try
{
Cons.SetColor(GetColor(type), ConsoleColor.Black);
Console.WriteLine(LOG_FORMAT, DateTime.Now.ToString(TIME_FORMAT), type.ToString(), message);
Cons.ResetColor();
//取消单独线程输出日志文件(单独线程输出日志必然会有延迟)
//if (IsWriteFile) Queue.Enqueue(new LogModel() { Type = type, Message = message, CreateTime = DateTime.Now });
}
catch { }
}
///
/// 写出到日志文件
///
///
private void WriteFile(LogModel log)
{
lock (LogFileLock)
{
//设置日志目录和日志文件
string filePath = GetFilePath(log.Type);
string file = DirTool.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
//创建日志目录
DirTool.Create(filePath);
//处理日志信息(换行)
//string[] loglines = log.Message.Split(new[] { "\r\n", "\n\r", Environment.NewLine }, StringSplitOptions.None);
//log.Message = String.Join($"{Environment.NewLine}{new string(' ', 18)}└", loglines);
//写出日志
TxtTool.Append(
file,
string.Format(LOG_FORMAT,
log.CreateTime.ToString(TIME_FORMAT),
log.Type.ToString(),
log.Message));
Cleaner(log.Type);
}
}
///
/// 根据分类分配目录
///
///
///
private string GetFilePath(LogType type)
{
string filePath = LogPath;
switch (type)
{
case LogType.d: filePath = DirTool.Combine(LogPath, "debug"); break;
case LogType.i: filePath = DirTool.Combine(LogPath, "information"); break;
case LogType.e: filePath = DirTool.Combine(LogPath, "error"); break;
case LogType.w: filePath = DirTool.Combine(LogPath, "warning"); break;
case LogType.v: filePath = DirTool.Combine(LogPath, "verbose"); break;
}
return filePath;
}
///
/// 清理过多的日志文件
///
private void Cleaner(LogType type)
{
List files = FileTool.GetFile(GetFilePath(type));
if (ListTool.HasElements(files))
{
files.ForEach(f =>
{
try
{
string filename = Path.GetFileNameWithoutExtension(f);
if (filename.Length == 10)
{
DateTime date = DateTime.Parse(filename);
if (date < DateTime.Now.AddDays(-CACHE_DAYS - 1)) FileTool.Delete(f);
}
else { FileTool.Delete(f); }
}
catch { FileTool.Delete(f); }
});
}
}
#region 分类详细输出
#region 因大小写命名规范冲突,将次类方法标记为弃用
///
/// 输出 verbose (啰嗦信息)
///
/// 消息
[Obsolete]
public void v(T msg)
{
V(msg);
}
///
/// 输出 Debug (调试信息)
///
/// 消息
[Obsolete]
public void d(T msg)
{
D(msg);
}
///
/// 输出 Information (重要信息)
///
/// 消息
[Obsolete]
public void i(T msg)
{
I(msg);
}
///
/// 输出 Warning (警告信息)
///
/// 消息
[Obsolete]
public void w(T msg)
{
W(msg);
}
///
/// 输出 Error (错误信息)
///
/// 消息
[Obsolete]
public void e(T msg)
{
E(msg);
}
#endregion
///
/// 输出 verbose (啰嗦信息)
///
/// 消息
public void V(T msg)
{
if ((ConsoleLogLevel & LogLevel.Verbose) == LogLevel.Verbose)
WriteConsole(LogType.v, msg.ToString());
if ((FileLogLevel & LogLevel.Verbose) == LogLevel.Verbose)
WriteFile(new LogModel() { Type = LogType.v, Message = msg.ToString(), CreateTime = DateTime.Now });
}
///
/// 输出 Debug (调试信息)
///
/// 消息
public void D(T msg)
{
if ((ConsoleLogLevel & LogLevel.Debug) == LogLevel.Debug)
WriteConsole(LogType.d, msg.ToString());
if ((FileLogLevel & LogLevel.Debug) == LogLevel.Debug)
WriteFile(new LogModel() { Type = LogType.d, Message = msg.ToString(), CreateTime = DateTime.Now });
}
///
/// 输出 Information (重要信息)
///
/// 消息
public void I(T msg)
{
if ((ConsoleLogLevel & LogLevel.Information) == LogLevel.Information)
WriteConsole(LogType.i, msg.ToString());
if ((FileLogLevel & LogLevel.Information) == LogLevel.Information)
WriteFile(new LogModel() { Type = LogType.i, Message = msg.ToString(), CreateTime = DateTime.Now });
}
///
/// 输出 Warning (警告信息)
///
/// 消息
public void W(T msg)
{
if ((ConsoleLogLevel & LogLevel.Warning) == LogLevel.Warning)
WriteConsole(LogType.w, msg.ToString());
if ((FileLogLevel & LogLevel.Warning) == LogLevel.Warning)
WriteFile(new LogModel() { Type = LogType.w, Message = msg.ToString(), CreateTime = DateTime.Now });
}
///
/// 输出 Error (错误信息)
///
/// 消息
public void E(T msg)
{
if ((ConsoleLogLevel & LogLevel.Error) == LogLevel.Error)
WriteConsole(LogType.e, msg.ToString());
if ((FileLogLevel & LogLevel.Error) == LogLevel.Error)
WriteFile(new LogModel() { Type = LogType.e, Message = msg.ToString(), CreateTime = DateTime.Now });
}
#endregion
}
}