2025-11-11 13:51:23 +09:00
|
|
|
#nullable enable
|
|
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-11 19:24:08 +09:00
|
|
|
using log4net;
|
2025-06-10 20:16:35 +09:00
|
|
|
using log4net.Appender;
|
|
|
|
|
using log4net.Core;
|
|
|
|
|
using log4net.Filter;
|
|
|
|
|
using log4net.Layout;
|
|
|
|
|
using log4net.Repository.Hierarchy;
|
2025-11-11 13:51:23 +09:00
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UVC.Log
|
|
|
|
|
{
|
|
|
|
|
public static class ULog
|
|
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
private static readonly ILog logger = LogManager.GetLogger(typeof(ULog));
|
2025-11-11 13:51:23 +09:00
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
|
|
|
|
|
private static readonly bool useUnityDebug = true; // Unity Debug 사용 여부
|
|
|
|
|
|
|
|
|
|
static ULog()
|
|
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (!useUnityDebug)
|
|
|
|
|
{
|
|
|
|
|
// unity runtime 일때
|
|
|
|
|
if (Application.platform == RuntimePlatform.WindowsPlayer)
|
|
|
|
|
{
|
|
|
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Path.Combine(Application.dataPath, @"Resources\log4net.runtime.xml")));
|
|
|
|
|
}
|
2025-06-11 19:24:08 +09:00
|
|
|
// unity editor 일때
|
2025-06-10 20:16:35 +09:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Path.Combine(Application.dataPath, @"Resources\log4net.editor.xml")));
|
|
|
|
|
//FileConfigure();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Debug(string msg)
|
|
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (logger.IsDebugEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (useUnityDebug)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.Log(msg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StackFrame frame = (new StackTrace(true)).GetFrame(1);
|
|
|
|
|
var method = frame.GetMethod();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
var name = method.Name;
|
|
|
|
|
int lineNumber = frame.GetFileLineNumber();
|
|
|
|
|
|
|
|
|
|
logger.Debug(type.Name + "." + name + " [line " + lineNumber + "] " + msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#else
|
|
|
|
|
UnityEngine.Debug.Log(msg);
|
|
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Info(string msg)
|
|
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (logger.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (useUnityDebug)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.Log(msg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StackFrame frame = (new StackTrace(true)).GetFrame(1);
|
|
|
|
|
var method = frame.GetMethod();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
var name = method.Name;
|
|
|
|
|
|
|
|
|
|
int lineNumber = frame.GetFileLineNumber();
|
|
|
|
|
logger.Info(type.Name + "." + name + " [line " + lineNumber + "] " + msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#else
|
|
|
|
|
UnityEngine.Debug.Log(msg);
|
|
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 19:24:08 +09:00
|
|
|
public static void Warning(string msg, Exception? ex = null)
|
2025-06-10 20:16:35 +09:00
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (logger.IsWarnEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (useUnityDebug)
|
|
|
|
|
{
|
2025-06-11 19:24:08 +09:00
|
|
|
UnityEngine.Debug.LogWarning(msg + (ex != null ? ", " + ex: ""));
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StackFrame frame = (new StackTrace(true)).GetFrame(1);
|
|
|
|
|
var method = frame.GetMethod();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
var name = method.Name;
|
|
|
|
|
int lineNumber = frame.GetFileLineNumber();
|
|
|
|
|
|
|
|
|
|
logger.Warn(type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#else
|
|
|
|
|
UnityEngine.Debug.LogWarning(msg);
|
|
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 19:24:08 +09:00
|
|
|
public static void Error(string msg, Exception? ex = null)
|
2025-06-10 20:16:35 +09:00
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (logger.IsErrorEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (useUnityDebug)
|
|
|
|
|
{
|
2025-06-11 19:24:08 +09:00
|
|
|
UnityEngine.Debug.LogError(msg + (ex != null ? ", " + ex : ""));
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StackFrame frame = (new StackTrace(true)).GetFrame(1);
|
|
|
|
|
var method = frame.GetMethod();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
var name = method.Name;
|
|
|
|
|
int lineNumber = frame.GetFileLineNumber();
|
|
|
|
|
|
|
|
|
|
logger.Error(type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#else
|
|
|
|
|
if (ex != null) UnityEngine.Debug.LogError($"{msg}\n{ex}");
|
|
|
|
|
else UnityEngine.Debug.LogError(msg);
|
|
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 19:24:08 +09:00
|
|
|
public static void Fatal(string msg, Exception? ex = null)
|
2025-06-10 20:16:35 +09:00
|
|
|
{
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
if (logger.IsFatalEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (useUnityDebug)
|
|
|
|
|
{
|
2025-06-11 19:24:08 +09:00
|
|
|
UnityEngine.Debug.LogError(msg + (ex != null ? ", " + ex : ""));
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StackFrame frame = (new StackTrace(true)).GetFrame(1);
|
|
|
|
|
var method = frame.GetMethod();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
var name = method.Name;
|
|
|
|
|
int lineNumber = frame.GetFileLineNumber();
|
|
|
|
|
|
|
|
|
|
logger.Fatal(type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#else
|
|
|
|
|
if (ex != null) UnityEngine.Debug.LogError($"{msg}\n{ex}");
|
|
|
|
|
else UnityEngine.Debug.LogError(msg);
|
|
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
|
2025-11-11 13:51:23 +09:00
|
|
|
#if !UNITY_WEBGL || UNITY_EDITOR
|
2025-06-10 20:16:35 +09:00
|
|
|
/// <summary>
|
|
|
|
|
/// log4net 파일 설정을 구성합니다.
|
|
|
|
|
/// Assets/Resources/log4net.editor.xml log4net.runtime.xml 파일로 구성하였기에 사용안함.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void FileConfigure()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var hierarchy = (Hierarchy)LogManager.GetRepository();
|
|
|
|
|
|
|
|
|
|
var patternLayout = new PatternLayout();
|
|
|
|
|
patternLayout.ConversionPattern = "[%date][%thread][%level][%logger] %message%newline";
|
|
|
|
|
patternLayout.ActivateOptions();
|
|
|
|
|
|
|
|
|
|
//var folder = new DirectoryInfo(Path.Combine(Application.streamingAssetsPath, "unityLogs"));
|
2025-06-23 20:06:15 +09:00
|
|
|
//if (!folder.Exists) folder.GetOrCreate();
|
2025-06-10 20:16:35 +09:00
|
|
|
|
|
|
|
|
var fileAppender = new RollingFileAppender
|
|
|
|
|
{
|
|
|
|
|
File = Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"Logs\"),
|
|
|
|
|
DatePattern = "yyyy-MM-dd'.log'",
|
|
|
|
|
AppendToFile = true,
|
|
|
|
|
RollingStyle = RollingFileAppender.RollingMode.Date,
|
|
|
|
|
StaticLogFileName = false,
|
|
|
|
|
Layout = patternLayout,
|
|
|
|
|
};
|
|
|
|
|
fileAppender.AddFilter(new LevelRangeFilter() { LevelMin = Level.Info, LevelMax = Level.Fatal });
|
|
|
|
|
fileAppender.ActivateOptions();
|
|
|
|
|
|
|
|
|
|
var fatalFileAppender = new RollingFileAppender
|
|
|
|
|
{
|
|
|
|
|
File = Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"Logs\fatal.log"),
|
|
|
|
|
MaxSizeRollBackups = 100,
|
|
|
|
|
MaximumFileSize = "10MB",
|
|
|
|
|
AppendToFile = true,
|
|
|
|
|
RollingStyle = RollingFileAppender.RollingMode.Size,
|
|
|
|
|
StaticLogFileName = true,
|
|
|
|
|
Layout = patternLayout,
|
|
|
|
|
};
|
|
|
|
|
fatalFileAppender.AddFilter(new LevelRangeFilter() { LevelMin = Level.Fatal, LevelMax = Level.Fatal });
|
|
|
|
|
fatalFileAppender.ActivateOptions();
|
|
|
|
|
|
|
|
|
|
hierarchy.Root.AddAppender(fileAppender);
|
|
|
|
|
hierarchy.Root.AddAppender(fatalFileAppender);
|
|
|
|
|
|
|
|
|
|
//var appender = new UnityDefaultLogAppender();
|
|
|
|
|
//appender.Layout = patternLayout;
|
|
|
|
|
//hierarchy.Root.AddAppender(appender);
|
|
|
|
|
|
|
|
|
|
hierarchy.Root.Level = Level.All;
|
|
|
|
|
hierarchy.Configured = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-11 13:51:23 +09:00
|
|
|
#endif
|
2025-06-10 20:16:35 +09:00
|
|
|
}
|
|
|
|
|
}
|