Skip to content

Commit f1db833

Browse files
Merge pull request #437 from splunk/DVPL-10608
added setup_logging() method in splunklib for logging and updated README.md files
2 parents 41ae251 + fbeff52 commit f1db833

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ class GeneratorTest(GeneratingCommand):
230230
checkpoint_dir = inputs.metadata["checkpoint_dir"]
231231
```
232232

233+
#### Optional:Set up logging for splunklib
234+
+ The default level is WARNING, which means that only events of this level and above will be visible
235+
+ To change a logging level we can call setup_logging() method and pass the logging level as an argument.
236+
+ Optional: we can also pass log format and date format string as a method argument to modify default format
237+
238+
```python
239+
import logging
240+
from splunklib import setup_logging
241+
242+
# To see debug and above level logs
243+
setup_logging(logging.DEBUG)
244+
```
245+
233246
### Changelog
234247

235248
The [CHANGELOG](CHANGELOG.md) contains a description of changes for each version of the SDK. For the latest version, see the [CHANGELOG.md](https://github.com/splunk/splunk-sdk-python/blob/master/CHANGELOG.md) on GitHub.

examples/searchcommands_app/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The app is tested on Splunk 5 and 6. Here is its manifest:
3535
└── default.meta ............. Permits the search assistant to use searchbnf.conf[6]
3636
```
3737
**References**
38-
[1] [app.conf](https://docs.splunk.com/Documentation/Splunk/latest/Admin/Appconf app.conf)
38+
[1] [app.conf](https://docs.splunk.com/Documentation/Splunk/latest/Admin/Appconf)
3939
[2] [commands.conf](https://docs.splunk.com/Documentation/Splunk/latest/Admin/Commandsconf)
4040
[3] [Python Logging HOWTO](https://docs.python.org/2/howto/logging.html)
4141
[4] [ConfigParser—Configuration file parser](https://docs.python.org/2/library/configparser.html)
@@ -110,6 +110,15 @@ word_counts |
110110
:-----|
111111
4497.0 |
112112

113+
## Optional:Set up logging using logging.conf file
114+
+ Inside the **default** directory of our app, we have a [logging.conf](https://github.com/splunk/splunk-sdk-python/blob/master/examples/searchcommands_app/package/default/logging.conf) file.
115+
+ In logging.conf file we can define loggers, handlers and formatters for our app. refer [this doc](https://docs.python.org/2/library/logging.config.html#configuration-file-format) for more details
116+
+ Logs will be written in the files specified in the handlers defined for the respective loggers
117+
+ For **'searchcommands_app'** app logs will be written in **searchcommands_app.log** and **splunklib.log** files defined in respective handlers, and are present at $SPLUNK_HOME/etc/apps/searchcommands_app/ dir
118+
+ By default logs will be written in the app's root directory, but it can be overriden by specifying the absolute path for the logs file in the conf file
119+
+ By default, logging level is set to WARNING
120+
+ To see debug and above level logs, Set level to DEBUG in logging.conf file
121+
113122
## License
114123

115124
This software is licensed under the Apache License 2.0. Details can be found in

examples/searchcommands_app/package/default/logging.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ keys = searchcommands
9696

9797
[formatter_searchcommands]
9898
format = %(asctime)s, Level=%(levelname)s, Pid=%(process)s, Logger=%(name)s, File=%(filename)s, Line=%(lineno)s, %(message)s
99+
datefmt = %Y-%m-%d %H:%M:%S %Z

splunklib/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,20 @@
1616

1717
from __future__ import absolute_import
1818
from splunklib.six.moves import map
19+
import logging
20+
21+
DEFAULT_LOG_FORMAT = '%(asctime)s, Level=%(levelname)s, Pid=%(process)s, Logger=%(name)s, File=%(filename)s, ' \
22+
'Line=%(lineno)s, %(message)s'
23+
DEFAULT_DATE_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
24+
25+
26+
# To set the logging level of splunklib
27+
# ex. To enable debug logs, call this method with parameter 'logging.DEBUG'
28+
# default logging level is set to 'WARNING'
29+
def setup_logging(level, log_format=DEFAULT_LOG_FORMAT, date_format=DEFAULT_DATE_FORMAT):
30+
logging.basicConfig(level=level,
31+
format=log_format,
32+
datefmt=date_format)
33+
1934
__version_info__ = (1, 6, 18)
2035
__version__ = ".".join(map(str, __version_info__))

splunklib/binding.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
except ImportError as e:
4848
from xml.parsers.expat import ExpatError as ParseError
4949

50+
logger = logging.getLogger(__name__)
5051

5152
__all__ = [
5253
"AuthenticationError",
@@ -68,7 +69,7 @@ def new_f(*args, **kwargs):
6869
start_time = datetime.now()
6970
val = f(*args, **kwargs)
7071
end_time = datetime.now()
71-
logging.debug("Operation took %s", end_time-start_time)
72+
logger.debug("Operation took %s", end_time-start_time)
7273
return val
7374
return new_f
7475

@@ -616,7 +617,7 @@ def delete(self, path_segment, owner=None, app=None, sharing=None, **query):
616617
"""
617618
path = self.authority + self._abspath(path_segment, owner=owner,
618619
app=app, sharing=sharing)
619-
logging.debug("DELETE request to %s (body: %s)", path, repr(query))
620+
logger.debug("DELETE request to %s (body: %s)", path, repr(query))
620621
response = self.http.delete(path, self._auth_headers, **query)
621622
return response
622623

@@ -679,7 +680,7 @@ def get(self, path_segment, owner=None, app=None, headers=None, sharing=None, **
679680

680681
path = self.authority + self._abspath(path_segment, owner=owner,
681682
app=app, sharing=sharing)
682-
logging.debug("GET request to %s (body: %s)", path, repr(query))
683+
logger.debug("GET request to %s (body: %s)", path, repr(query))
683684
all_headers = headers + self.additional_headers + self._auth_headers
684685
response = self.http.get(path, all_headers, **query)
685686
return response
@@ -757,7 +758,7 @@ def post(self, path_segment, owner=None, app=None, sharing=None, headers=None, *
757758
headers = []
758759

759760
path = self.authority + self._abspath(path_segment, owner=owner, app=app, sharing=sharing)
760-
logging.debug("POST request to %s (body: %s)", path, repr(query))
761+
logger.debug("POST request to %s (body: %s)", path, repr(query))
761762
all_headers = headers + self.additional_headers + self._auth_headers
762763
response = self.http.post(path, all_headers, **query)
763764
return response
@@ -826,7 +827,7 @@ def request(self, path_segment, method="GET", headers=None, body={},
826827
app=app, sharing=sharing)
827828

828829
all_headers = headers + self.additional_headers + self._auth_headers
829-
logging.debug("%s request to %s (headers: %s, body: %s)",
830+
logger.debug("%s request to %s (headers: %s, body: %s)",
830831
method, path, str(all_headers), repr(body))
831832

832833
if body:

splunklib/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
namespace)
7676
from .data import record
7777

78+
logger = logging.getLogger(__name__)
79+
7880
__all__ = [
7981
"connect",
8082
"NotSupportedError",
@@ -1476,7 +1478,7 @@ def iter(self, offset=0, count=None, pagesize=None, **kwargs):
14761478
if pagesize is None or N < pagesize:
14771479
break
14781480
offset += N
1479-
logging.debug("pagesize=%d, fetched=%d, offset=%d, N=%d, kwargs=%s", pagesize, fetched, offset, N, kwargs)
1481+
logger.debug("pagesize=%d, fetched=%d, offset=%d, N=%d, kwargs=%s", pagesize, fetched, offset, N, kwargs)
14801482

14811483
# kwargs: count, offset, search, sort_dir, sort_key, sort_mode
14821484
def list(self, count=None, **kwargs):
@@ -2545,9 +2547,9 @@ def list(self, *kinds, **kwargs):
25452547
kinds = self.kinds
25462548
if len(kinds) == 1:
25472549
kind = kinds[0]
2548-
logging.debug("Inputs.list taking short circuit branch for single kind.")
2550+
logger.debug("Inputs.list taking short circuit branch for single kind.")
25492551
path = self.kindpath(kind)
2550-
logging.debug("Path for inputs: %s", path)
2552+
logger.debug("Path for inputs: %s", path)
25512553
try:
25522554
path = UrlEncoded(path, skip_encode=True)
25532555
response = self.get(path, **kwargs)

0 commit comments

Comments
 (0)