-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingBus.cs
243 lines (211 loc) · 9.09 KB
/
LoggingBus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//-----------------------------------------------------------------------
// <copyright file="LoggingBus.cs" company="Akka.NET Project">
// Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
// Copyright (C) 2013-2015 Akka.NET project <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.Configuration;
namespace Akka.Event
{
/// <summary>
/// Represents a logging bus which subscribes loggers to the system LogEvents for the desired minimum level.
/// </summary>
public class LoggingBus : ActorEventBus<object, Type>
{
private static readonly LogLevel[] AllLogLevels = Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>().ToArray();
private static int _loggerId;
private readonly List<IActorRef> _loggers = new List<IActorRef>();
/// <summary>
/// Gets the minimum log level that this LoggingBus will subscribe to, any LogEvents with a log level below will not be subscribed to.
/// </summary>
/// <value>The log level.</value>
public LogLevel LogLevel { get; private set; }
/// <summary>
/// Determines whether [is sub classification] [the specified parent].
/// </summary>
/// <param name="parent">The parent.</param>
/// <param name="child">The child.</param>
/// <returns><c>true</c> if [is sub classification] [the specified parent]; otherwise, <c>false</c>.</returns>
protected override bool IsSubClassification(Type parent, Type child)
{
return parent.IsAssignableFrom(child);
}
/// <summary>
/// Publishes the specified event.
/// </summary>
/// <param name="event">The event.</param>
/// <param name="subscriber">The subscriber.</param>
protected override void Publish(object @event, IActorRef subscriber)
{
subscriber.Tell(@event);
}
/// <summary>
/// Classifies the specified event.
/// </summary>
/// <param name="event">The event.</param>
/// <param name="classifier">The classifier.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
protected override bool Classify(object @event, Type classifier)
{
return classifier.IsAssignableFrom(GetClassifier(@event));
}
/// <summary>
/// Gets the classifier for the LogEvent.
/// </summary>
/// <param name="event">The event.</param>
/// <returns>Type.</returns>
protected override Type GetClassifier(object @event)
{
return @event.GetType();
}
/// <summary>
/// Starts the default loggers.
/// </summary>
/// <param name="system">The system.</param>
/// <exception cref="System.Exception">Can not use logger of type: + loggerType</exception>
internal void StartDefaultLoggers(ActorSystemImpl system)
{
var logName = SimpleName(this) + "(" + system.Name + ")";
var logLevel = Logging.LogLevelFor(system.Settings.LogLevel);
var loggerTypes = system.Settings.Loggers;
var timeout = system.Settings.LoggerStartTimeout;
var shouldRemoveStandardOutLogger = true;
foreach (var strLoggerType in loggerTypes)
{
var loggerType = Type.GetType(strLoggerType);
if (loggerType == null)
{
throw new ConfigurationException("Logger specified in config cannot be found: \"" + strLoggerType + "\"");
}
if (loggerType == typeof(StandardOutLogger))
{
shouldRemoveStandardOutLogger = false;
continue;
}
try
{
AddLogger(system, loggerType, logLevel, logName, timeout);
}
catch (Exception e)
{
throw new ConfigurationException(string.Format("Logger [{0}] specified in config cannot be loaded: {1}", strLoggerType, e),e);
}
}
LogLevel = logLevel;
if (system.Settings.DebugUnhandledMessage)
{
var forwarder = system.SystemActorOf(Props.Create(typeof(UnhandledMessageForwarder)), "UnhandledMessageForwarder");
Subscribe(forwarder, typeof(UnhandledMessage));
}
if (shouldRemoveStandardOutLogger)
{
Publish(new Debug(logName, GetType(), "StandardOutLogger being removed"));
Unsubscribe(Logging.StandardOutLogger);
}
Publish(new Debug(logName, GetType(), "Default Loggers started"));
}
internal void StopDefaultLoggers(ActorSystem system)
{
//TODO: Implement stopping loggers
}
private void AddLogger(ActorSystemImpl system, Type loggerType, LogLevel logLevel, string loggingBusName, TimeSpan timeout)
{
var loggerName = CreateLoggerName(loggerType);
var logger = system.SystemActorOf(Props.Create(loggerType), loggerName);
var askTask = logger.Ask(new InitializeLogger(this));
if (!askTask.Wait(timeout))
{
Publish(new Warning(loggingBusName, GetType(),
string.Format("Logger {0} [{2}] did not respond within {1} to InitializeLogger(bus)", loggerName, timeout, loggerType.FullName)));
}
else
{
var response = askTask.Result;
if (!(response is LoggerInitialized))
{
throw new LoggerInitializationException(string.Format("Logger {0} [{2}] did not respond with LoggerInitialized, sent instead {1}", loggerName, response, loggerType.FullName));
}
_loggers.Add(logger);
SubscribeLogLevelAndAbove(logLevel, logger);
Publish(new Debug(loggingBusName, GetType(), string.Format("Logger {0} [{1}] started", loggerName, loggerType.Name)));
}
}
private string CreateLoggerName(Type actorClass)
{
var id = Interlocked.Increment(ref _loggerId);
var name = "log" + id + "-" + SimpleName(actorClass);
return name;
}
/// <summary>
/// Starts the StandardOutLogger logger.
/// </summary>
/// <param name="config">The configuration.</param>
public void StartStdoutLogger(Settings config)
{
SetUpStdoutLogger(config);
Publish(new Debug(SimpleName(this), GetType(), "StandardOutLogger started"));
}
/// <summary>
/// Sets up StandardOutLogger logger.
/// </summary>
/// <param name="config">The configuration.</param>
private void SetUpStdoutLogger(Settings config)
{
var logLevel = Logging.LogLevelFor(config.StdoutLogLevel);
SubscribeLogLevelAndAbove(logLevel, Logging.StandardOutLogger);
}
/// <summary>
/// Sets the minimum log level for the LoggingBus, any LogEvents below this level will not be listened to.
/// </summary>
/// <param name="logLevel">The log level.</param>
public void SetLogLevel(LogLevel logLevel)
{
LogLevel = logLevel;
foreach (var logger in _loggers)
{
//subscribe to given log level and above
SubscribeLogLevelAndAbove(logLevel, logger);
//unsubscribe to all levels below loglevel
foreach (var level in AllLogLevels.Where(l => l < logLevel))
{
Unsubscribe(logger, level.ClassFor());
}
}
}
private void SubscribeLogLevelAndAbove(LogLevel logLevel, IActorRef logger)
{
//subscribe to given log level and above
foreach (var level in AllLogLevels.Where(l => l >= logLevel))
{
Subscribe(logger, level.ClassFor());
}
}
private class UnhandledMessageForwarder : ActorBase
{
protected override bool Receive(object message)
{
var msg = message as UnhandledMessage;
if (msg == null)
return false;
Context.System.EventStream.Publish(ToDebug(msg));
return true;
}
private static Debug ToDebug(UnhandledMessage message)
{
var msg = string.Format(
CultureInfo.InvariantCulture, "Unhandled message from {0} : {1}",
message.Sender.Path,
message.Message
);
return new Debug(message.Recipient.Path.ToString(), message.Recipient.GetType(), msg);
}
}
}
}