-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLogger.cs
More file actions
161 lines (143 loc) · 5.62 KB
/
Copy pathLogger.cs
File metadata and controls
161 lines (143 loc) · 5.62 KB
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
// Copyright 2017 The Nuclio Authors.
//
// 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 System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Nuclio.Sdk
{
public class Logger
{
public event EventHandler LogEvent;
[DataMember(Name = "level")]
public string Level { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
[DataMember(Name = "datetime")]
public string DateTime { get; set; }
[DataMember(Name = "with")]
public Dictionary<string, object> With { get; set; }
///<summary>
/// Log an error message
/// e.g. context.Logger.Error("{0} not responding after {1} seconds", dbHost, timeout)
/// <param name="format">Message format</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void Error(string format, params object[] args)
{
Log(LogLevel.Error, string.Format(format, args));
}
///<summary>
/// Log a warning message
/// e.g. context.Logger.Warning("{0} {1} full", "memory", mem_full)
/// <param name="format">Message format</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void Warning(string format, params object[] args)
{
Log(LogLevel.Warning, string.Format(format, args));
}
///<summary>
/// Log a debug message
/// e.g. context.Logger.Debug("event with {0} bytes", event.GetSize())
/// <param name="format">Message format</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void Debug(string format, params object[] args)
{
Log(LogLevel.Debug, string.Format(format, args));
}
///<summary>
/// Log an info message
/// e.g. context.Logger.Info("event with {0} bytes", event.GetSize())
/// <param name="format">Message format</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void Info(string format, params object[] args)
{
Log(LogLevel.Info, string.Format(format, args));
}
///<summary>
/// Log an error message
/// e.g. context.Logger.ErrorWith("bad request", "error", "daffy not found", "time", 7)
/// <param name="format">Message</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void ErrorWith(string message, params object[] parameters)
{
Log(LogLevel.Error, message, EncodeWith(parameters));
}
///<summary>
/// Log an error message
/// e.g. context.Logger.WarnWith("system overload", "resource", "memory", "used", 0.9)
/// <param name="format">Message</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void WarnWith(string message, params object[] parameters)
{
Log(LogLevel.Warning, message, EncodeWith(parameters));
}
///<summary>
/// Log an error message
/// e.g. context.Logger.DebugWith("event", "body_size", 2339, "content-type", "text/plain")
/// <param name="format">Message</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void DebugWith(string message, params object[] parameters)
{
Log(LogLevel.Debug, message, EncodeWith(parameters));
}
///<summary>
/// Log an error message
/// e.g. context.Logger.InfoWith("event processed", "time", 0.3, "count", 9009)
/// <param name="format">Message</param>
/// <param name="args">Formatting arguments</param>
///</summary>
public void InfoWith(string message, params object[] parameters)
{
Log(LogLevel.Info, message, EncodeWith(parameters));
}
private enum LogLevel
{
Error,
Warning,
Info,
Debug
}
private Dictionary<string, object> EncodeWith(object[] with)
{
var withMap = new Dictionary<string, object>();
if (with.Length % 2 != 0)
{
Console.WriteLine($"error: bad width length - {with.Length}");
return withMap;
}
for (int i = 0; i < with.Length; i += 2)
{
withMap.Add(with[i].ToString(), with[i + 1]);
}
return withMap;
}
private void Log(LogLevel level, string message, Dictionary<string, object> with = null)
{
DateTime = (System.DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
Level = level.ToString().ToLower();
Message = message;
if (with == null)
with = new Dictionary<string, object>();
With = with;
if (LogEvent != null)
LogEvent(this, new EventArgs());
}
}
}