• R/O
  • HTTP
  • SSH
  • HTTPS

newslash: Commit

newslash


Commit MetaInfo

Revisão475dd9030832bc0179339ef2ef9da44ef28e19a2 (tree)
Hora2019-06-19 17:46:07
Autorhylom <hylom@user...>
Commiterhylom

Mensagem de Log

searchd: hide verbose message and add '-v' option to searchd_cli

Mudança Sumário

Diff

--- a/src/ns_search/searchd_cli.py
+++ b/src/ns_search/searchd_cli.py
@@ -8,6 +8,7 @@ import os
88 import sys
99 import time
1010 import inspect
11+import logging
1112
1213 from yaml import load
1314 try:
@@ -26,6 +27,7 @@ class SearchCLIError(Exception):
2627 class SearchCLI(object):
2728 def __init__(self):
2829 self.index_dir = None
30+ self.logger = logging.getLogger()
2931 self._parse_args()
3032
3133 config_path = os.environ.get("SEARCHD_CONFIG", "/etc/newslash/searchd.conf")
@@ -41,6 +43,7 @@ class SearchCLI(object):
4143 # parse command line option
4244 parser = argparse.ArgumentParser(description='search daemon for Newslash')
4345 parser.add_argument('-i', '--index-dir', help='lucene index directory')
46+ parser.add_argument('-v', '--verbose', action='store_true', help='show more log messages')
4447
4548 # subcommands
4649 subparsers = parser.add_subparsers(help="subcommands")
@@ -132,10 +135,10 @@ class SearchCLI(object):
132135 index = Index(database=self.database, index_path=self.index_dir)
133136
134137 def progress_cb(target, phase, success, errors):
135- print("{}: {} to index {} items... ({} errors)".format(target, phase, success, errors))
138+ self.logger.info("{} {} index for {} items... ({} errors)".format(phase, target, success, errors))
136139
137140 def error_cb(target, phase, item):
138- print("{}: indexing {} error: id={}".format(target, phase, item["sid"]))
141+ self.logger.error("{} {} index error: id={}".format(phase, target, item["sid"]))
139142
140143 if action == 'clear':
141144 index.delete_all()
@@ -157,11 +160,17 @@ class SearchCLI(object):
157160 index.update_poll(progress_cb=progress_cb, error_cb=error_cb)
158161 elif target == 'user':
159162 index.update_user(progress_cb=progress_cb, error_cb=error_cb)
160- print("indexing done. total time: {}s".format(time.time() - start_time))
163+ else:
164+ self.logger.error("invalid target - {}".format(target))
165+
166+ self.logger.info("indexing done. total time: {}s".format(time.time() - start_time))
167+
168+ else:
169+ self.logger.error("invalid action - {}".format(action))
161170
162171 def query(self):
163172 if self.index_dir is None:
164- sys.stderr.write("error: index directory not given\n")
173+ self.logger.error("index directory not given")
165174 return
166175
167176 query_string = self.args.query_string
@@ -171,7 +180,7 @@ class SearchCLI(object):
171180 try:
172181 query = lucene_wrapper.Query("content_text", query_string)
173182 except lucene_wrapper.QueryParseError as e:
174- sys.stderr.write("query parse error\n")
183+ self.logger.error("query parse error")
175184 return
176185
177186 result = searcher.search(query)
@@ -182,7 +191,7 @@ class SearchCLI(object):
182191
183192 def analyze(self):
184193 if self.index_dir is None:
185- sys.stderr.write("error: index directory not given\n")
194+ self.logger.error("index directory not given")
186195 return
187196
188197 query_string = self.args.query_string
@@ -199,7 +208,7 @@ class SearchCLI(object):
199208 query.add_should(content_query)
200209 query.add_should(title_query)
201210 except lucene_wrapper.QueryParseError as e:
202- sys.stderr.write("query parse error\n")
211+ self.logger.error("query parse error")
203212 return
204213
205214 sort = lucene_wrapper.Sort("create_time", lucene_wrapper.Sort.INT, True)
@@ -232,7 +241,7 @@ class SearchCLI(object):
232241 return
233242
234243 if result.totalhits > 1:
235- print("warning: hits multiple items")
244+ self.logger.warning("hits multiple items")
236245
237246 print("#{} - {}: {}".format(result.number, result.id, result.content_text.encode('utf-8')))
238247
@@ -241,9 +250,15 @@ class SearchCLI(object):
241250 try:
242251 index.metadata.create_table()
243252 except DatabaseError as e:
244- print('error: {}'.format(str(e)))
253+ self.logger.error('{}'.format(str(e)))
245254
246255 def run(self):
256+ # set loglevel
257+ if self.args.verbose:
258+ self.logger.setLevel(logging.DEBUG)
259+ else:
260+ self.logger.setLevel(logging.WARNING)
261+
247262 if self.sub_command == 'query':
248263 return self.query()
249264
@@ -266,5 +281,6 @@ class SearchCLI(object):
266281
267282
268283 if __name__ == '__main__':
284+ logging.basicConfig(format='%(asctime)s[%(levelname)s] %(message)s', level=logging.DEBUG)
269285 cli = SearchCLI()
270286 cli.run()
Show on old repository browser