Skip to content

Commit fbeff52

Browse files
committed
add formatter for logs and update README.md file
- add default time and log formatter in setup_logging() method - add details of setup_logging() method and logging.conf files in README.md file
1 parent ec06849 commit fbeff52

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

README.md

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

246+
#### Optional:Set up logging for splunklib
247+
+ The default level is WARNING, which means that only events of this level and above will be visible
248+
+ To change a logging level we can call setup_logging() method and pass the logging level as an argument.
249+
+ Optional: we can also pass log format and date format string as a method argument to modify default format
250+
251+
```python
252+
import logging
253+
from splunklib import setup_logging
254+
255+
# To see debug and above level logs
256+
setup_logging(logging.DEBUG)
257+
```
258+
246259
### Changelog
247260

248261
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
from splunklib.six.moves import map
1919
import logging
2020

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+
2126
# To set the logging level of splunklib
2227
# ex. To enable debug logs, call this method with parameter 'logging.DEBUG'
2328
# default logging level is set to 'WARNING'
24-
def setLoggingLevel(level):
25-
logging.basicConfig(level=level)
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)
2633

2734
__version_info__ = (1, 6, 18)
2835
__version__ = ".".join(map(str, __version_info__))

0 commit comments

Comments
 (0)