Danro/Assets/Scripts/Utilities/LoggerInitializer.cs
Danil Iarosh 98d5285b46 feat(logging): add advanced logging example with file output
- Created AdvancedLoggingExample class demonstrating composite and file loggers
- Implemented multi-file logging setup with separate error and full logs
- Added game event and performance logging methods
- Included key input handling for log operations (flush, clear)
- Setup proper resource disposal on application quit
2025-11-07 14:12:50 +03:00

93 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
namespace RR.Common.Utility
{
/// <summary>
/// Пример инициализации системы логирования с CompositeLogger и FileLogger.
/// Прикрепите этот скрипт к любому GameObject на сцене.
/// </summary>
public class LoggerInitializer : MonoBehaviour
{
[SerializeField] private bool enableFileLogging = true;
[SerializeField] private string customLogPath = ""; // Оставьте пустым для использования пути по умолчанию
[SerializeField] private bool appendToExistingLog = true;
[SerializeField] private bool clearLogOnStart = false;
private CompositeLogger compositeLogger;
private FileLogger fileLogger;
private void Awake()
{
InitializeLogging();
}
private void InitializeLogging()
{
// Создаем композитный логгер
compositeLogger = new CompositeLogger();
compositeLogger.Initialize();
if (enableFileLogging)
{
// Создаем файловый логгер
string logPath = string.IsNullOrEmpty(customLogPath) ? null : customLogPath;
fileLogger = new FileLogger(logPath, appendToExistingLog);
if (clearLogOnStart)
{
fileLogger.Clear();
}
// Добавляем файловый логгер в композитный
compositeLogger.AddLogger(fileLogger);
Debug.Log(
$"[LoggerInitializer] Файловое логирование включено. Путь: {(logPath ?? Application.persistentDataPath + "/game.log")}");
}
// Опционально: можно заменить стандартный логгер Unity
// Debug.unityLogger = compositeLogger;
// Тестовые сообщения
TestLogging();
}
private void TestLogging()
{
compositeLogger.Log("Система логирования инициализирована");
compositeLogger.LogWarning("Test", "Это тестовое предупреждение");
compositeLogger.LogError("Test", "Это тестовая ошибка");
Debug.Log($"Логи сохраняются в: {Application.persistentDataPath}");
}
private void OnApplicationQuit()
{
// Закрываем файловый логгер при выходе
fileLogger?.Dispose();
Debug.Log("[LoggerInitializer] Файловый логгер закрыт");
}
private void OnDestroy()
{
// На всякий случай закрываем при уничтожении объекта
fileLogger?.Dispose();
}
// Публичные методы для использования из других скриптов
public static void LogToFile(string message)
{
Debug.Log(message);
}
public static void LogWarningToFile(string message)
{
Debug.LogWarning(message);
}
public static void LogErrorToFile(string message)
{
Debug.LogError(message);
}
}
}