Skip to content

Commit a9707e7

Browse files
author
David Noble
committed
DVPL-3653: splunklib.searchcommands | logging_level option is broken
Verified by automated unit tests and ad-hoc integration tests. Signed-off-by: David Noble <[email protected]>
1 parent 546388b commit a9707e7

File tree

3 files changed

+111
-19
lines changed

3 files changed

+111
-19
lines changed

splunklib/searchcommands/logging.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from logging.config import fileConfig
1818
from logging import getLogger
19-
import inspect
2019
import os
2120

2221

splunklib/searchcommands/search_command.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ordereddict import OrderedDict # python 2.6
2323

2424
from inspect import getmembers
25+
from logging import getLevelName
2526
from os import path
2627
from sys import argv, stdin, stdout
2728

@@ -85,24 +86,24 @@ def __str__(self):
8586
# self.logger, self._logging_configuration = logging.configure(
8687
# type(self).__name__, value)
8788
# return
88-
#
89-
# @Option
90-
# def logging_level(self):
91-
# """ **Syntax:** logging_level=[CRITICAL|ERROR|WARNING|INFO|DEBUG|NOTSET]
92-
#
93-
# **Description:** Sets the threshold for the logger of this command
94-
# invocation. Logging messages less severe than `logging_level` will be
95-
# ignored.
96-
#
97-
# """
98-
# return self.logger.getEffectiveLevel()
99-
#
100-
# @logging_level.setter
101-
# def logging_level(self, value):
102-
# if value is None:
103-
# value = self._default_logging_level
104-
# self.logger.setLevel(value)
105-
# return
89+
90+
@Option
91+
def logging_level(self):
92+
""" **Syntax:** logging_level=[CRITICAL|ERROR|WARNING|INFO|DEBUG|NOTSET]
93+
94+
**Description:** Sets the threshold for the logger of this command
95+
invocation. Logging messages less severe than `logging_level` will be
96+
ignored.
97+
98+
"""
99+
return getLevelName(self.logger.getEffectiveLevel())
100+
101+
@logging_level.setter
102+
def logging_level(self, value):
103+
if value is None:
104+
value = self._default_logging_level
105+
self.logger.setLevel(value if type(value) is not str else value.upper())
106+
return
106107

107108
show_configuration = Option(doc='''
108109
**Syntax:** show_configuration=<bool>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2013 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
try:
18+
import unittest2 as unittest
19+
except ImportError:
20+
import unittest
21+
22+
from splunklib.searchcommands.search_command_internals import SearchCommandParser
23+
from splunklib.searchcommands import Configuration, StreamingCommand
24+
25+
import logging
26+
27+
@Configuration()
28+
class SearchCommand(StreamingCommand):
29+
30+
def stream(self, records):
31+
pass
32+
33+
34+
class TestSearchCommandsDecorators(unittest.TestCase):
35+
36+
def setUp(self):
37+
super(TestSearchCommandsDecorators, self).setUp()
38+
return
39+
40+
def test_builtin_options(self):
41+
42+
# logging_level accepts all logging levels and returns their canonical
43+
# string values
44+
45+
warning = logging.getLevelName(logging.WARNING)
46+
notset = logging.getLevelName(logging.NOTSET)
47+
command = SearchCommand()
48+
49+
self.assertEquals(command.logging_level, warning)
50+
51+
for level in logging._levelNames:
52+
if type(level) is int:
53+
command.logging_level = level
54+
level_name = logging.getLevelName(level)
55+
self.assertEquals(command.logging_level, warning if level_name == notset else level_name)
56+
else:
57+
level_name = logging.getLevelName(logging.getLevelName(level))
58+
for variant in level, level.lower(), level.capitalize():
59+
command.logging_level = variant
60+
self.assertEquals(command.logging_level, warning if level_name == notset else level_name)
61+
62+
# show_configuration accepts Splunk boolean values
63+
64+
boolean_values = {
65+
'0': False, '1': True,
66+
'f': False, 't': True,
67+
'n': False, 'y': True,
68+
'no': False, 'yes': True,
69+
'false': False, 'true': True}
70+
71+
for value in boolean_values:
72+
for variant in [value, value.capitalize(), value.upper()]:
73+
command.show_configuration = variant
74+
self.assertEquals(command.show_configuration, boolean_values[value])
75+
76+
for value in 'any-other-string', 13:
77+
try:
78+
command.show_configuration = value
79+
except ValueError:
80+
pass
81+
except Exception as e:
82+
self.fail('Expected ValueError, but a %s was raised' % type(e))
83+
else:
84+
self.fail('Expected ValueError, but show_configuration=%s' % command.show_configuration)
85+
86+
# SearchCommandParser recognizes each built-in option
87+
88+
for argv in ['logging_level=DEBUG'], ['show_configuration=true']:
89+
parser = SearchCommandParser()
90+
parser.parse(argv, command)
91+
92+
return

0 commit comments

Comments
 (0)