Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions Appegy.UniLogger.Lab/Assets/Scripts/ULoggerInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static class ULoggerInitializer
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void AutoConfigureLogger()
{
// Disable stacktrace for logs and warnings in build
if (!Application.isEditor)
{
Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
Expand All @@ -17,43 +16,24 @@ private static void AutoConfigureLogger()
Application.SetStackTraceLogType(LogType.Assert, StackTraceLogType.ScriptOnly);
}

// Initialize ULogger and the unity console target
InitializeUnityTarget();

// Mirror logs into a rolling file
InitializeFileTarget();
}

private static void InitializeUnityTarget()
{
// Customize formatter for logs unity target
var formatter = Application.isEditor
? new Formatter(FormatOptions.RichText | FormatOptions.Tags)
: new Formatter(FormatOptions.Tags | FormatOptions.LogType);

// Prepare filterer for unity target (by default all logs are allowed)
var filterer = new Filterer(true);

// Now you can disable logs in filterer by log's level or tag
// For example
// Disable all Trace logs:
// filterer.Disable(LogLevel.Trace);
// Disable all logs with tag Unsorted
// filterer.Mute("Unsorted");

// When formatter and filterer are ready - initialize logger and add the unity console target
ULogger.Initialize();
ULogger.AddTarget(new UnityTarget(formatter, filterer));
ULogger.Configure(c => c
.WriteTo.Unity(t => t
.Format(Application.isEditor
? FormatOptions.RichText | FormatOptions.Tags
: FormatOptions.Tags | FormatOptions.LogType)
.Filter(Filterer.AllowAllTags()))
.WriteTo.File(t => t
.Path(LogFilePath())
.Format(FormatOptions.Time | FormatOptions.Tags | FormatOptions.LogType)
.RollEvery(1024 * 1024)
.Keep(5)));
}

private static void InitializeFileTarget()
private static string LogFilePath()
{
// In the editor keep logs next to the Assets folder; in a build use the persistent data path
var path = Application.isEditor
return Application.isEditor
? Path.Combine(Application.dataPath, "..", "Logs", "game.log")
: Path.Combine(Application.persistentDataPath, "Logs", "game.log");
var formatter = new Formatter(FormatOptions.Time | FormatOptions.Tags | FormatOptions.LogType);
ULogger.AddTarget(new FileTarget(path, fileSizeLimitBytes: 1024 * 1024, retainedFileCountLimit: 5, formatter: formatter));
}
}
}
65 changes: 65 additions & 0 deletions src/Runtime/LoggerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace Appegy.UniLogger
{
public sealed class LoggerConfiguration
{
private readonly List<Target> _targets = new();

public LoggerConfiguration()
{
WriteTo = new LoggerSinkConfiguration(this);
}

public LoggerSinkConfiguration WriteTo { get; }

internal IReadOnlyList<Target> BuildTargets() => _targets;

internal LoggerConfiguration Add(Target target)
{
_targets.Add(target);
return this;
}
}

public sealed class LoggerSinkConfiguration
{
private readonly LoggerConfiguration _configuration;

internal LoggerSinkConfiguration(LoggerConfiguration configuration)
{
_configuration = configuration;
}

public LoggerConfiguration Unity([CanBeNull] Action<UnityTargetBuilder> configure = null)
{
var builder = new UnityTargetBuilder();
configure?.Invoke(builder);
return _configuration.Add(builder.Build());
}

public LoggerConfiguration File([NotNull] Action<FileTargetBuilder> configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
var builder = new FileTargetBuilder();
configure.Invoke(builder);
return _configuration.Add(builder.Build());
}

public LoggerConfiguration Memory([NotNull] Action<MemoryTargetBuilder> configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
var builder = new MemoryTargetBuilder();
configure.Invoke(builder);
return _configuration.Add(builder.Build());
}

public LoggerConfiguration Target([NotNull] Target target)
{
if (target == null) throw new ArgumentNullException(nameof(target));
return _configuration.Add(target);
}
}
}
2 changes: 2 additions & 0 deletions src/Runtime/LoggerConfiguration.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/Runtime/Targets/File/FileTargetBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using JetBrains.Annotations;

namespace Appegy.UniLogger
{
public sealed class FileTargetBuilder
{
private string _path;
private FormatOptions _format = FormatOptions.None;
private Filterer _filterer;
private long _rollEveryBytes;
private int _retainedFileCount;
private bool _autoFlush;

public FileTargetBuilder Path([NotNull] string path)
{
_path = path;
return this;
}

public FileTargetBuilder Format(FormatOptions options)
{
_format = options;
return this;
}

public FileTargetBuilder Filter([CanBeNull] Filterer filterer)
{
_filterer = filterer;
return this;
}

public FileTargetBuilder RollEvery(long bytes)
{
_rollEveryBytes = bytes;
return this;
}

public FileTargetBuilder Keep(int count)
{
_retainedFileCount = count;
return this;
}

public FileTargetBuilder AutoFlush(bool enabled = true)
{
_autoFlush = enabled;
return this;
}

internal FileTarget Build()
{
if (string.IsNullOrEmpty(_path))
{
throw new InvalidOperationException("File target requires a path. Use .Path(...).");
}
var formatter = new Formatter(_format);
var filterer = _filterer ?? Filterer.AllowAllTags();
return new FileTarget(_path, _rollEveryBytes, _retainedFileCount, _autoFlush, formatter, filterer);
}
}
}
2 changes: 2 additions & 0 deletions src/Runtime/Targets/File/FileTargetBuilder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Runtime/Targets/Filterer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public Filterer(bool allTagsEnabledByDefault)
_snapshot = new Snapshot(logTypeEnabled, new HashSet<string>());
}

public static Filterer AllowAllTags() => new(true);

public static Filterer BlockAllTags() => new(false);

/// <summary>
/// Returns true when <see cref="logLevel"/> and <see cref="tag"/> are not filtered and allowed to show
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/Runtime/Targets/Memory/MemoryTargetBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using JetBrains.Annotations;

namespace Appegy.UniLogger
{
public sealed class MemoryTargetBuilder
{
private int _capacity;
private FormatOptions _format = FormatOptions.None;
private Filterer _filterer;

public MemoryTargetBuilder Capacity(int capacity)
{
_capacity = capacity;
return this;
}

public MemoryTargetBuilder Format(FormatOptions options)
{
_format = options;
return this;
}

public MemoryTargetBuilder Filter([CanBeNull] Filterer filterer)
{
_filterer = filterer;
return this;
}

internal InMemoryTarget Build()
{
if (_capacity <= 0)
{
throw new InvalidOperationException("Memory target requires a positive capacity. Use .Capacity(...).");
}
var formatter = new Formatter(_format);
var filterer = _filterer ?? Filterer.AllowAllTags();
return new InMemoryTarget(_capacity, formatter, filterer);
}
}
}
2 changes: 2 additions & 0 deletions src/Runtime/Targets/Memory/MemoryTargetBuilder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/Runtime/Targets/Unity/UnityTargetBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using JetBrains.Annotations;

namespace Appegy.UniLogger
{
public sealed class UnityTargetBuilder
{
private FormatOptions _format = FormatOptions.None;
private Filterer _filterer;

public UnityTargetBuilder Format(FormatOptions options)
{
_format = options;
return this;
}

public UnityTargetBuilder Filter([CanBeNull] Filterer filterer)
{
_filterer = filterer;
return this;
}

internal UnityTarget Build()
{
var formatter = new Formatter(_format);
var filterer = _filterer ?? Filterer.AllowAllTags();
return new UnityTarget(formatter, filterer);
}
}
}
2 changes: 2 additions & 0 deletions src/Runtime/Targets/Unity/UnityTargetBuilder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 0 additions & 77 deletions src/Runtime/Types/LoggerConfig.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Runtime/Types/LoggerConfig.cs.meta

This file was deleted.

Loading
Loading