Skip to content

Commit 74eb664

Browse files
author
David Noble
committed
splunklib.searchcommands update
Made logging configuration files specific to a search command class, not an application
1 parent f97d715 commit 74eb664

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

splunklib/searchcommands/logging.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,62 @@
2020
import os
2121

2222

23-
def configure(instance, path=None):
24-
""" Configure logging for the app containing a class and get its logger
23+
def configure(name, path=None):
24+
""" Configure logging and return a logger and the location of its logging
25+
configuration file
2526
26-
This function expects a Splunk app directory structure:
27+
This function expects:
28+
29+
+ A Splunk app directory structure::
2730
2831
<app-root>
2932
bin
30-
<module>.py
3133
...
3234
default
33-
logging.conf
3435
...
3536
local
36-
logging.conf
3737
...
3838
39-
The **logging.conf** file must be in ConfigParser-format. The current
40-
working directory is set to *<app-root>* before the logging.conf file is
41-
loaded. Hence, relative path names should be set relative to *<app-root>*.
39+
+ The current working directory is *<app-root>***/bin**.
40+
41+
Splunk guarantees this. If you are running the app outside of Splunk, be
42+
sure to set the current working directory to *<app-root>***/bin** before
43+
calling.
44+
45+
This function looks for a logging configuration file at each of these
46+
locations, loading the first, if any, logging configuration file that it
47+
finds::
4248
43-
The current directory is reset to its previous value before this function
44-
returns.
49+
local/{name}.logging.conf
50+
default/{name}.logging.conf
51+
local/logging.conf
52+
default/logging.conf
53+
54+
The current working directory is set to *<app-root>* before the logging
55+
configuration file is loaded. Hence, paths in the logging configuration
56+
file are relative to *<app-root>*. The current directory is reset before
57+
return.
58+
59+
You may short circuit the search for a logging configuration file by
60+
providing an alternative file location in `path`. Logging configuration
61+
files must be in `ConfigParser format`_.
4562
4663
#Arguments:
4764
48-
:param cls: Class contained in <app-root>/bin/<module>.py
49-
:type cls: type
65+
:param name: Logger name
66+
:type name: str
5067
:param path: Location of an alternative logging configuration file or `None`
5168
:type path: str or NoneType
69+
:returns: A logger and the location of its logging configuration file
70+
71+
.. _ConfigParser format: http://goo.gl/K6edZ8
5272
5373
"""
54-
cls = type(instance)
55-
logger_name = cls.__name__
56-
module = inspect.getmodule(cls)
57-
app_directory = os.path.dirname(os.path.dirname(module.__file__))
74+
app_directory = os.path.dirname(os.getcwd())
5875
if path is None:
5976
probing_path = [
60-
'local/%s.logging.conf' % logger_name,
61-
'default/%s.logging.conf' % logger_name,
77+
'local/%s.logging.conf' % name,
78+
'default/%s.logging.conf' % name,
6279
'local/logging.conf',
6380
'default/logging.conf']
6481
for relative_path in probing_path:
@@ -74,5 +91,5 @@ def configure(instance, path=None):
7491
fileConfig(path)
7592
finally:
7693
os.chdir(working_directory)
77-
logger = getLogger(logger_name)
94+
logger = getLogger(name)
7895
return logger, path

splunklib/searchcommands/search_command.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def __init__(self):
4040

4141
# Variables that may be used, but not altered by derived classes
4242

43-
self.logger, self._logging_configuration = logging.configure(self)
43+
self.logger, self._logging_configuration = logging.configure(
44+
type(self).__name__)
4445
self.input_header = InputHeader()
4546
self.messages = MessagesHeader()
4647

@@ -77,7 +78,7 @@ def logging_configuration(self):
7778
@logging_configuration.setter
7879
def logging_configuration(self, value):
7980
self.logger, self._logging_configuration = logging.configure(
80-
self, value)
81+
type(self).__name__, value)
8182
return
8283

8384
@Option

0 commit comments

Comments
 (0)