diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2017-08-09 11:32:36 +0200 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@google.com> | 2017-08-20 17:27:01 +0200 |
commit | 1ca3bef6b65a3991957481fe38bba1740709c7f3 (patch) | |
tree | d48e23dbd34cf207b9f091274084e62609483a07 /src | |
parent | 71c468b566259a5ed18cf8e1f166bad7e4b878d9 (diff) |
make C# logging & tracing more intuitive
Diffstat (limited to 'src')
-rw-r--r-- | src/csharp/Grpc.Core/GrpcEnvironment.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Logging/LogLevel.cs | 6 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs | 47 |
3 files changed, 51 insertions, 4 deletions
diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs index cbc7d2078c..80031cb7ef 100644 --- a/src/csharp/Grpc.Core/GrpcEnvironment.cs +++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs @@ -43,7 +43,7 @@ namespace Grpc.Core static readonly HashSet<Channel> registeredChannels = new HashSet<Channel>(); static readonly HashSet<Server> registeredServers = new HashSet<Server>(); - static ILogger logger = new NullLogger(); + static ILogger logger = new LogLevelFilterLogger(new ConsoleLogger(), LogLevel.Off, true); readonly GrpcThreadPool threadPool; readonly DebugStats debugStats = new DebugStats(); diff --git a/src/csharp/Grpc.Core/Logging/LogLevel.cs b/src/csharp/Grpc.Core/Logging/LogLevel.cs index 7718e3c2ab..4db0e47b97 100644 --- a/src/csharp/Grpc.Core/Logging/LogLevel.cs +++ b/src/csharp/Grpc.Core/Logging/LogLevel.cs @@ -39,6 +39,10 @@ namespace Grpc.Core.Logging /// <summary> /// Error severity. /// </summary> - Error + Error, + /// <summary> + /// Logging is off. + /// </summary> + Off = int.MaxValue } } diff --git a/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs b/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs index b611c191a5..a9c78a4a7a 100644 --- a/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs +++ b/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs @@ -27,16 +27,31 @@ namespace Grpc.Core.Logging /// <summary>Logger that filters out messages below certain log level.</summary> public class LogLevelFilterLogger : ILogger { + // Verbosity environment variable used by C core. + private const string CoreVerbosityEnvVarName = "GRPC_VERBOSITY"; readonly ILogger innerLogger; readonly LogLevel logLevel; /// <summary> /// Creates and instance of <c>LogLevelFilter.</c> /// </summary> - public LogLevelFilterLogger(ILogger logger, LogLevel logLevel) + public LogLevelFilterLogger(ILogger logger, LogLevel logLevel) : this(logger, logLevel, false) + { + } + + /// <summary> + /// Creates and instance of <c>LogLevelFilter.</c> + /// The <c>fromEnvironmentVariable</c> parameter allows looking up "GRPC_VERBOSITY" setting provided by C-core + /// and uses the same log level for C# logs. Using this setting is recommended as it makes the otherwise separate + /// C# and C-core logging settings work in lockstep and more intutively. + /// </summary> + /// <param name="logger">the logger to forward filtered logs to.</param> + /// <param name="defaultLogLevel">the default log level, unless overriden by env variable.</param> + /// <param name="fromEnvironmentVariable">if <c>true</c>, override log level with setting from environment variable.</param> + public LogLevelFilterLogger(ILogger logger, LogLevel defaultLogLevel, bool fromEnvironmentVariable) { this.innerLogger = GrpcPreconditions.CheckNotNull(logger); - this.logLevel = logLevel; + this.logLevel = GetLogLevelFromEnvironment(defaultLogLevel, fromEnvironmentVariable); } /// <summary> @@ -141,5 +156,33 @@ namespace Grpc.Core.Logging innerLogger.Error(exception, message); } } + + /// <summary>Get log level based on a default and lookup of <c>GRPC_VERBOSITY</c> environment variable.</summary> + private static LogLevel GetLogLevelFromEnvironment(LogLevel defaultLogLevel, bool fromEnvironmentVariable) + { + if (!fromEnvironmentVariable) + { + return defaultLogLevel; + } + + var verbosityString = System.Environment.GetEnvironmentVariable(CoreVerbosityEnvVarName); + if (verbosityString == null) + { + return defaultLogLevel; + } + + // NOTE: C core doesn't have "WARNING" log level + switch (verbosityString.ToUpperInvariant()) + { + case "DEBUG": + return LogLevel.Debug; + case "INFO": + return LogLevel.Info; + case "ERROR": + return LogLevel.Error; + default: + return defaultLogLevel; + } + } } } |