Skip to content

Commit f496605

Browse files
committed
Add logging to exceptions in desperation, remove duplicated test (will restore if found needed), miscellaneous
1 parent 9ff33ee commit f496605

File tree

15 files changed

+294
-38
lines changed

15 files changed

+294
-38
lines changed

splunklib/binding.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def mask_sensitive_data(data):
102102
if not isinstance(data, dict):
103103
try:
104104
data = json.loads(data)
105-
except Exception as ex:
105+
except Exception as e:
106+
logging.error(e)
106107
return data
107108

108109
# json.loads will return "123"(str) as 123(int), so return the data if it's not 'dict' type
@@ -1400,14 +1401,16 @@ def request(self, url, message, **kwargs):
14001401
try:
14011402
response = self.handler(url, message, **kwargs)
14021403
break
1403-
except Exception:
1404+
except Exception as e:
1405+
logging.error(e)
14041406
if self.retries <= 0:
14051407
raise
14061408
else:
14071409
time.sleep(self.retryDelay)
14081410
self.retries -= 1
14091411
response = record(response)
14101412
if 400 <= response.status:
1413+
logging.error(response)
14111414
raise HTTPError(response)
14121415

14131416
# Update the cookie with any HTTP request

splunklib/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ def restart(self, timeout=None):
621621
if not self.restart_required:
622622
return result
623623
except Exception as e:
624+
logging.error(e)
624625
sleep(1)
625626
raise Exception("Operation time out.")
626627

splunklib/modularinput/script.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# under the License.
1414

1515
from abc import ABCMeta, abstractmethod
16+
import logging
1617
import sys
1718
import xml.etree.ElementTree as ET
1819
from urllib.parse import urlsplit
@@ -87,6 +88,7 @@ def run_script(self, args, event_writer, input_stream):
8788
self.validate_input(validation_definition)
8889
return 0
8990
except Exception as e:
91+
logging.error(e)
9092
root = ET.Element("error")
9193
ET.SubElement(root, "message").text = str(e)
9294
event_writer.write_xml_document(root)
@@ -99,6 +101,7 @@ def run_script(self, args, event_writer, input_stream):
99101
return 1
100102

101103
except Exception as e:
104+
logging.error(e)
102105
event_writer.log_exception(str(e))
103106
return 1
104107

splunklib/searchcommands/search_command.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import csv
2020
import io
21+
import logging
2122
import os
2223
import re
2324
import sys
@@ -971,6 +972,7 @@ def _read_chunk(istream):
971972
try:
972973
header = istream.readline()
973974
except Exception as error:
975+
logging.error(error)
974976
raise RuntimeError(f"Failed to read transport header: {error}")
975977

976978
if not header:
@@ -988,6 +990,7 @@ def _read_chunk(istream):
988990
try:
989991
metadata = istream.read(metadata_length)
990992
except Exception as error:
993+
logging.error(error)
991994
raise RuntimeError(
992995
f"Failed to read metadata of length {metadata_length}: {error}"
993996
)
@@ -997,6 +1000,7 @@ def _read_chunk(istream):
9971000
try:
9981001
metadata = decoder.decode(ensure_str(metadata))
9991002
except Exception as error:
1003+
logging.error(error)
10001004
raise RuntimeError(
10011005
f"Failed to parse metadata of length {metadata_length}: {error}"
10021006
)
@@ -1009,6 +1013,7 @@ def _read_chunk(istream):
10091013
if body_length > 0:
10101014
body = istream.read(body_length)
10111015
except Exception as error:
1016+
logging.error(error)
10121017
raise RuntimeError(f"Failed to read body of length {body_length}: {error}")
10131018

10141019
return metadata, ensure_str(body, errors="replace")

tests/searchcommands/chunked_data_stream.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import collections
21
import csv
32
import io
43
import json
4+
from collections import Iterable, Iterator
55

66
import splunklib.searchcommands.internals
77
from splunklib.utils import ensure_binary, ensure_str
@@ -15,7 +15,7 @@ def __init__(self, version, meta, data):
1515
self.data = csv.DictReader(io.StringIO(data.decode("utf-8")), dialect=dialect)
1616

1717

18-
class ChunkedDataStreamIter(collections.abc.Iterator):
18+
class ChunkedDataStreamIter(Iterator):
1919
def __init__(self, chunk_stream):
2020
self.chunk_stream = chunk_stream
2121

@@ -29,7 +29,7 @@ def next(self):
2929
raise StopIteration
3030

3131

32-
class ChunkedDataStream(collections.abc.Iterable):
32+
class ChunkedDataStream(Iterable):
3333
def __iter__(self):
3434
return ChunkedDataStreamIter(self)
3535

tests/searchcommands/test_apps/eventing_app/bin/eventingcsc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
# License for the specific language governing permissions and limitations
1616
# under the License.
1717

18-
import os, sys
18+
import os
19+
import sys
1920

2021
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
2122
from splunklib.searchcommands import (
22-
dispatch,
23-
EventingCommand,
2423
Configuration,
24+
EventingCommand,
2525
Option,
26-
validators,
26+
dispatch,
2727
)
2828

2929

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# The format and semantics of this file are described in this article at Python.org:
3+
#
4+
# [Configuration file format](https://docs.python.org/2/library/logging.config.html#configuration-file-format)
5+
#
6+
[loggers]
7+
keys = root, splunklib, SearchCommand
8+
9+
[logger_root]
10+
# Default: WARNING
11+
level = DEBUG
12+
# Default: stderr
13+
handlers = stderr
14+
15+
[logger_splunklib]
16+
qualname = splunklib
17+
# Default: WARNING
18+
level = NOTSET
19+
# Default: stderr
20+
handlers = splunklib
21+
# Default: 1
22+
propagate = 0
23+
24+
[logger_SearchCommand]
25+
qualname = SearchCommand
26+
# Default: WARNING
27+
level = NOTSET
28+
# Default: stderr
29+
handlers = app
30+
# Default: 1
31+
propagate = 0
32+
33+
[handlers]
34+
# See [logging.handlers](https://docs.python.org/2/library/logging.handlers.html)
35+
keys = app, splunklib, stderr
36+
37+
[handler_app]
38+
# Select this handler to log events to app.log
39+
class = logging.handlers.RotatingFileHandler
40+
level = NOTSET
41+
args = ('app.log', 'a', 524288000, 9, 'utf-8', True)
42+
formatter = searchcommands
43+
44+
[handler_splunklib]
45+
# Select this handler to log events to splunklib.log
46+
class = logging.handlers.RotatingFileHandler
47+
args = ('splunklib.log', 'a', 524288000, 9, 'utf-8', True)
48+
level = NOTSET
49+
formatter = searchcommands
50+
51+
[handler_stderr]
52+
# Select this handler to log events to stderr which splunkd redirects to the associated job's search.log file
53+
class = logging.StreamHandler
54+
level = NOTSET
55+
args = (sys.stderr,)
56+
formatter = searchcommands
57+
58+
[formatters]
59+
keys = searchcommands
60+
61+
[formatter_searchcommands]
62+
format = %(asctime)s, Level=%(levelname)s, Pid=%(process)s, File=%(filename)s, Line=%(lineno)s, %(message)s

tests/searchcommands/test_apps/generating_app/bin/generatingcsc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
# License for the specific language governing permissions and limitations
1616
# under the License.
1717

18-
import os, sys
18+
import os
19+
import sys
1920
import time
2021

2122
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
2223
from splunklib.searchcommands import (
23-
dispatch,
24-
GeneratingCommand,
2524
Configuration,
25+
GeneratingCommand,
2626
Option,
27+
dispatch,
2728
validators,
2829
)
2930

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# The format and semantics of this file are described in this article at Python.org:
3+
#
4+
# [Configuration file format](https://docs.python.org/2/library/logging.config.html#configuration-file-format)
5+
#
6+
[loggers]
7+
keys = root, splunklib, SearchCommand
8+
9+
[logger_root]
10+
# Default: WARNING
11+
level = DEBUG
12+
# Default: stderr
13+
handlers = stderr
14+
15+
[logger_splunklib]
16+
qualname = splunklib
17+
# Default: WARNING
18+
level = NOTSET
19+
# Default: stderr
20+
handlers = splunklib
21+
# Default: 1
22+
propagate = 0
23+
24+
[logger_SearchCommand]
25+
qualname = SearchCommand
26+
# Default: WARNING
27+
level = NOTSET
28+
# Default: stderr
29+
handlers = app
30+
# Default: 1
31+
propagate = 0
32+
33+
[handlers]
34+
# See [logging.handlers](https://docs.python.org/2/library/logging.handlers.html)
35+
keys = app, splunklib, stderr
36+
37+
[handler_app]
38+
# Select this handler to log events to app.log
39+
class = logging.handlers.RotatingFileHandler
40+
level = NOTSET
41+
args = ('app.log', 'a', 524288000, 9, 'utf-8', True)
42+
formatter = searchcommands
43+
44+
[handler_splunklib]
45+
# Select this handler to log events to splunklib.log
46+
class = logging.handlers.RotatingFileHandler
47+
args = ('splunklib.log', 'a', 524288000, 9, 'utf-8', True)
48+
level = NOTSET
49+
formatter = searchcommands
50+
51+
[handler_stderr]
52+
# Select this handler to log events to stderr which splunkd redirects to the associated job's search.log file
53+
class = logging.StreamHandler
54+
level = NOTSET
55+
args = (sys.stderr,)
56+
formatter = searchcommands
57+
58+
[formatters]
59+
keys = searchcommands
60+
61+
[formatter_searchcommands]
62+
format = %(asctime)s, Level=%(levelname)s, Pid=%(process)s, File=%(filename)s, Line=%(lineno)s, %(message)s
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# The format and semantics of this file are described in this article at Python.org:
3+
#
4+
# [Configuration file format](https://docs.python.org/2/library/logging.config.html#configuration-file-format)
5+
#
6+
[loggers]
7+
keys = root, splunklib, SearchCommand
8+
9+
[logger_root]
10+
# Default: WARNING
11+
level = DEBUG
12+
# Default: stderr
13+
handlers = stderr
14+
15+
[logger_splunklib]
16+
qualname = splunklib
17+
# Default: WARNING
18+
level = NOTSET
19+
# Default: stderr
20+
handlers = splunklib
21+
# Default: 1
22+
propagate = 0
23+
24+
[logger_SearchCommand]
25+
qualname = SearchCommand
26+
# Default: WARNING
27+
level = NOTSET
28+
# Default: stderr
29+
handlers = app
30+
# Default: 1
31+
propagate = 0
32+
33+
[handlers]
34+
# See [logging.handlers](https://docs.python.org/2/library/logging.handlers.html)
35+
keys = app, splunklib, stderr
36+
37+
[handler_app]
38+
# Select this handler to log events to app.log
39+
class = logging.handlers.RotatingFileHandler
40+
level = NOTSET
41+
args = ('app.log', 'a', 524288000, 9, 'utf-8', True)
42+
formatter = searchcommands
43+
44+
[handler_splunklib]
45+
# Select this handler to log events to splunklib.log
46+
class = logging.handlers.RotatingFileHandler
47+
args = ('splunklib.log', 'a', 524288000, 9, 'utf-8', True)
48+
level = NOTSET
49+
formatter = searchcommands
50+
51+
[handler_stderr]
52+
# Select this handler to log events to stderr which splunkd redirects to the associated job's search.log file
53+
class = logging.StreamHandler
54+
level = NOTSET
55+
args = (sys.stderr,)
56+
formatter = searchcommands
57+
58+
[formatters]
59+
keys = searchcommands
60+
61+
[formatter_searchcommands]
62+
format = %(asctime)s, Level=%(levelname)s, Pid=%(process)s, File=%(filename)s, Line=%(lineno)s, %(message)s

0 commit comments

Comments
 (0)