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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# Copyright (c) 2011, 2012 Nicira, 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.
import datetime
import logging
import logging.handlers
import re
import socket
import sys
import ovs.dirs
import ovs.unixctl
import ovs.util
FACILITIES = {"console": "info", "file": "info", "syslog": "info"}
LEVELS = {
"dbg": logging.DEBUG,
"info": logging.INFO,
"warn": logging.WARNING,
"err": logging.ERROR,
"emer": logging.CRITICAL,
"off": logging.CRITICAL
}
def get_level(level_str):
return LEVELS.get(level_str.lower())
class Vlog:
__inited = False
__msg_num = 0
__mfl = {} # Module -> facility -> level
__log_file = None
__file_handler = None
def __init__(self, name):
"""Creates a new Vlog object representing a module called 'name'. The
created Vlog object will do nothing until the Vlog.init() static method
is called. Once called, no more Vlog objects may be created."""
assert not Vlog.__inited
self.name = name.lower()
if name not in Vlog.__mfl:
Vlog.__mfl[self.name] = FACILITIES.copy()
def __log(self, level, message, **kwargs):
if not Vlog.__inited:
return
now = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
message = ("%s|%s|%s|%s|%s"
% (now, Vlog.__msg_num, self.name, level, message))
level = LEVELS.get(level.lower(), logging.DEBUG)
Vlog.__msg_num += 1
for f, f_level in Vlog.__mfl[self.name].iteritems():
f_level = LEVELS.get(f_level, logging.CRITICAL)
if level >= f_level:
logging.getLogger(f).log(level, message, **kwargs)
def emer(self, message, **kwargs):
self.__log("EMER", message, **kwargs)
def err(self, message, **kwargs):
self.__log("ERR", message, **kwargs)
def warn(self, message, **kwargs):
self.__log("WARN", message, **kwargs)
def info(self, message, **kwargs):
self.__log("INFO", message, **kwargs)
def dbg(self, message, **kwargs):
self.__log("DBG", message, **kwargs)
def exception(self, message):
"""Logs 'message' at ERR log level. Includes a backtrace when in
exception context."""
self.err(message, exc_info=True)
@staticmethod
def init(log_file=None):
"""Intializes the Vlog module. Causes Vlog to write to 'log_file' if
not None. Should be called after all Vlog objects have been created.
No logging will occur until this function is called."""
if Vlog.__inited:
return
Vlog.__inited = True
logging.raiseExceptions = False
Vlog.__log_file = log_file
for f in FACILITIES:
logger = logging.getLogger(f)
logger.setLevel(logging.DEBUG)
try:
if f == "console":
logger.addHandler(logging.StreamHandler(sys.stderr))
elif f == "syslog":
logger.addHandler(logging.handlers.SysLogHandler(
address="/dev/log",
facility=logging.handlers.SysLogHandler.LOG_DAEMON))
elif f == "file" and Vlog.__log_file:
Vlog.__file_handler = logging.FileHandler(Vlog.__log_file)
logger.addHandler(Vlog.__file_handler)
except (IOError, socket.error):
logger.setLevel(logging.CRITICAL)
ovs.unixctl.command_register("vlog/reopen", "", 0, 0,
Vlog._unixctl_vlog_reopen, None)
ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxint,
Vlog._unixctl_vlog_set, None)
ovs.unixctl.command_register("vlog/list", "", 0, 0,
Vlog._unixctl_vlog_list, None)
@staticmethod
def set_level(module, facility, level):
""" Sets the log level of the 'module'-'facility' tuple to 'level'.
All three arguments are strings which are interpreted the same as
arguments to the --verbose flag. Should be called after all Vlog
objects have already been created."""
module = module.lower()
facility = facility.lower()
level = level.lower()
if facility != "any" and facility not in FACILITIES:
return
if module != "any" and module not in Vlog.__mfl:
return
if level not in LEVELS:
return
if module == "any":
modules = Vlog.__mfl.keys()
else:
modules = [module]
if facility == "any":
facilities = FACILITIES.keys()
else:
facilities = [facility]
for m in modules:
for f in facilities:
Vlog.__mfl[m][f] = level
@staticmethod
def set_levels_from_string(s):
module = None
level = None
facility = None
for word in [w.lower() for w in re.split('[ :]', s)]:
if word == "any":
pass
elif word in FACILITIES:
if facility:
return "cannot specify multiple facilities"
facility = word
elif word in LEVELS:
if level:
return "cannot specify multiple levels"
level = word
elif word in Vlog.__mfl:
if module:
return "cannot specify multiple modules"
module = word
else:
return "no facility, level, or module \"%s\"" % word
Vlog.set_level(module or "any", facility or "any", level or "any")
@staticmethod
def get_levels():
lines = [" console syslog file\n",
" ------- ------ ------\n"]
lines.extend(sorted(["%-16s %4s %4s %4s\n"
% (m,
Vlog.__mfl[m]["console"],
Vlog.__mfl[m]["syslog"],
Vlog.__mfl[m]["file"]) for m in Vlog.__mfl]))
return ''.join(lines)
@staticmethod
def reopen_log_file():
"""Closes and then attempts to re-open the current log file. (This is
useful just after log rotation, to ensure that the new log file starts
being used.)"""
if Vlog.__log_file:
logger = logging.getLogger("file")
logger.removeHandler(Vlog.__file_handler)
Vlog.__file_handler = logging.FileHandler(Vlog.__log_file)
logger.addHandler(Vlog.__file_handler)
@staticmethod
def _unixctl_vlog_reopen(conn, unused_argv, unused_aux):
if Vlog.__log_file:
Vlog.reopen_log_file()
conn.reply(None)
else:
conn.reply("Logging to file not configured")
@staticmethod
def _unixctl_vlog_set(conn, argv, unused_aux):
for arg in argv:
msg = Vlog.set_levels_from_string(arg)
if msg:
conn.reply(msg)
return
conn.reply(None)
@staticmethod
def _unixctl_vlog_list(conn, unused_argv, unused_aux):
conn.reply(Vlog.get_levels())
def add_args(parser):
"""Adds vlog related options to 'parser', an ArgumentParser object. The
resulting arguments parsed by 'parser' should be passed to handle_args."""
group = parser.add_argument_group(title="Logging Options")
group.add_argument("--log-file", nargs="?", const="default",
help="Enables logging to a file. Default log file"
" is used if LOG_FILE is omitted.")
group.add_argument("-v", "--verbose", nargs="*",
help="Sets logging levels, see ovs-vswitchd(8)."
" Defaults to dbg.")
def handle_args(args):
""" Handles command line arguments ('args') parsed by an ArgumentParser.
The ArgumentParser should have been primed by add_args(). Also takes care
of initializing the Vlog module."""
log_file = args.log_file
if log_file == "default":
log_file = "%s/%s.log" % (ovs.dirs.LOGDIR, ovs.util.PROGRAM_NAME)
if args.verbose is None:
args.verbose = []
elif args.verbose == []:
args.verbose = ["any:any:dbg"]
for verbose in args.verbose:
msg = Vlog.set_levels_from_string(verbose)
if msg:
ovs.util.ovs_fatal(0, "processing \"%s\": %s" % (verbose, msg))
Vlog.init(log_file)
|