123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using System.IO;
- using UnityEngine;
- /* 本文件为日志系统的实现
- * 该系统用于记录游戏运行时的日志信息,包括普通日志、错误日志和警告日志
- * 日志信息将被保存到指定路径的文件中,便于后续查看和分析
- */
-
- public class LogSystem : MonoBehaviour
- {
- public static LogSystem Instance { get; private set; }
- private string logFilePath;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- // 设置日志文件路径
- DateTime now = DateTime.Now;
- logFilePath = Application.persistentDataPath + "/logs/" + now.ToString("yyyy-MM-dd") + UserProperty.userId + ".log";
- Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
- }
- public void Log(string message)
- {
- string logMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message;
- Debug.Log(logMessage);
- File.AppendAllText(logFilePath, logMessage + Environment.NewLine);
- }
- public void LogError(string message)
- {
- string logMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - ERROR: " + message;
- Debug.LogError(logMessage);
- File.AppendAllText(logFilePath, logMessage + Environment.NewLine);
- }
- public void LogWarning(string message)
- {
- string logMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - WARNING: " + message;
- Debug.LogWarning(logMessage);
- File.AppendAllText(logFilePath, logMessage + Environment.NewLine);
- }
- }
|