/*
Copyright 2017 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google.Apis.Util;
using System;
namespace Google.Apis.Logging
{
///
/// A logger than logs to StdError or StdOut.
///
public sealed class ConsoleLogger : BaseLogger, ILogger
{
///
/// Construct a .
///
/// Logging will be enabled at this level and all higher levels.
/// true to log to StdOut, defaults to logging to StdError.
/// Optional ; will use the system clock if null.
public ConsoleLogger(LogLevel minimumLogLevel, bool logToStdOut = false, IClock clock = null) : this(minimumLogLevel, logToStdOut, clock, null) { }
private ConsoleLogger(LogLevel minimumLogLevel, bool logToStdOut, IClock clock, Type forType) : base(minimumLogLevel, clock, forType)
{
LogToStdOut = logToStdOut;
}
///
/// false to log to StdError; true to log to StdOut.
///
public bool LogToStdOut { get; }
///
protected override ILogger BuildNewLogger(Type type) => new ConsoleLogger(MinimumLogLevel, LogToStdOut, Clock, type);
///
protected override void Log(LogLevel logLevel, string formattedMessage)
{
(LogToStdOut ? Console.Out : Console.Error).WriteLine(formattedMessage);
}
}
}