using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Zhaizj.Framework.Web;
using System.IO;
namespace Zhaizj.Framework.Log
{
///
/// 日志配置文件,默认配置文件在 /framework/config/log.config,日志文件在 /framework/log/log.txt 中
///
public class LogConfig
{
///
/// 日志配置信息(全局缓存)
///
/// logLevel 的值(不区分大小写):none, debug, info, warn, error, fatal, all;
/// logFile 和 logProvider 通常不用填写
///
///
/// 配置文件的格式(一行一条配置,键值之间用冒号分开)。
///
/// logLevel : info
/// logFile : log/log.txt
/// logProvider : Zhaizj.Framework.Log.FileLogger
///
///
///
public static readonly LogConfig Instance = new LogConfig();
///
/// 日志配置内容
///
public Dictionary dic = new Dictionary();
///
/// 构造函数初始化
///
private LogConfig()
{
String absPath = getConfigAbsPath();
dic = cfgHelper.Read(absPath);
this.FilePath = getFilePath(dic);
this.Level = getLevel(dic);
this.LoggerImpl = getLoggerImpl(dic);
}
///
/// 获取日志级别
///
///
///
private LogLevel getLevel(Dictionary dic)
{
String level;
dic.TryGetValue("logLevel", out level);
if (strUtil.IsNullOrEmpty(level)) return getDefaultLevel();
try
{
return (LogLevel)Enum.Parse(typeof(LogLevel), level, true);
}
catch { return getDefaultLevel(); }
}
///
/// 获取默认级别
///
///
private static LogLevel getDefaultLevel()
{
return LogLevel.None;
}
private void loadDefault()
{
this.Level = LogLevel.Debug;
this.FilePath = getAbsoluteLogPath(getFilePath(dic));
}
///
/// 获取配置文件路径
///
///
private static String getConfigAbsPath()
{
String absolutePath = PathHelper.Map(strUtil.Join(cfgHelper.ConfigRoot, "log.config"));
if (!File.Exists(absolutePath))
{
absolutePath = null;
}
return absolutePath;
}
///
/// 日志输出配置
///
///
///
private static String getFilePath(Dictionary dic)
{
String filePath;
String logPath;
dic.TryGetValue("logFile", out filePath);
dic.TryGetValue("logPath", out logPath);
filePath = getDefaultLogPath(filePath, logPath);
return getAbsoluteLogPath(filePath);
}
///
/// 日志输出路径修改
///
///
///
///
private static String getDefaultLogPath(string filePath, string logdir)
{
if (!Directory.Exists(logdir))
{
Directory.CreateDirectory(logdir);
}
string logPath = logdir + "/" + DateTime.Now.ToString(filePath) + ".txt";
return cfgHelper.FrameworkRoot + logPath;
}
private static String getAbsoluteLogPath(String path)
{
if (path.StartsWith(cfgHelper.FrameworkRoot) == false)
path = strUtil.Join(cfgHelper.FrameworkRoot, path);
return PathHelper.Map(path);
}
private static String getLoggerImpl(Dictionary dic)
{
String logProvider;
dic.TryGetValue("logProvider", out logProvider);
return logProvider;
}
//---------------------------------------------------------------------------------------------
///
/// 记录的层次,不区分大小写,有 none, debug, info, warn, error, fatal, all 这几种可选
///
public LogLevel Level { get; set; }
///
/// 日志文件存储的路径
///
public String FilePath { get; set; }
///
/// 日志记录工具,默认是 FileLogger
///
public String LoggerImpl { get; set; }
}
}