浏览代码

[enh] using the logger

Adam Tauber 10 年前
父节点
当前提交
299a80a1eb
共有 4 个文件被更改,包括 22 次插入8 次删除
  1. 7
    4
      searx/engines/__init__.py
  2. 5
    2
      searx/https_rewrite.py
  3. 5
    2
      searx/search.py
  4. 5
    0
      searx/webapp.py

+ 7
- 4
searx/engines/__init__.py 查看文件

@@ -22,6 +22,10 @@ from imp import load_source
22 22
 from flask.ext.babel import gettext
23 23
 from operator import itemgetter
24 24
 from searx import settings
25
+from searx import logger
26
+
27
+
28
+logger = logger.getChild('engines')
25 29
 
26 30
 engine_dir = dirname(realpath(__file__))
27 31
 
@@ -81,7 +85,7 @@ def load_engine(engine_data):
81 85
         if engine_attr.startswith('_'):
82 86
             continue
83 87
         if getattr(engine, engine_attr) is None:
84
-            print('[E] Engine config error: Missing attribute "{0}.{1}"'
88
+            logger.error('Missing engine config attribute: "{0}.{1}"'
85 89
                   .format(engine.name, engine_attr))
86 90
             sys.exit(1)
87 91
 
@@ -100,9 +104,8 @@ def load_engine(engine_data):
100 104
         categories['general'].append(engine)
101 105
 
102 106
     if engine.shortcut:
103
-        # TODO check duplications
104 107
         if engine.shortcut in engine_shortcuts:
105
-            print('[E] Engine config error: ambigious shortcut: {0}'
108
+            logger.error('Engine config error: ambigious shortcut: {0}'
106 109
                   .format(engine.shortcut))
107 110
             sys.exit(1)
108 111
         engine_shortcuts[engine.shortcut] = engine.name
@@ -199,7 +202,7 @@ def get_engines_stats():
199 202
 
200 203
 
201 204
 if 'engines' not in settings or not settings['engines']:
202
-    print '[E] Error no engines found. Edit your settings.yml'
205
+    logger.error('No engines found. Edit your settings.yml')
203 206
     exit(2)
204 207
 
205 208
 for engine_data in settings['engines']:

+ 5
- 2
searx/https_rewrite.py 查看文件

@@ -20,8 +20,11 @@ from urlparse import urlparse
20 20
 from lxml import etree
21 21
 from os import listdir
22 22
 from os.path import isfile, isdir, join
23
+from searx import logger
23 24
 
24 25
 
26
+logger = logger.getChild("https_rewrite")
27
+
25 28
 # https://gitweb.torproject.org/\
26 29
 # pde/https-everywhere.git/tree/4.0:/src/chrome/content/rules
27 30
 
@@ -131,7 +134,7 @@ def load_single_https_ruleset(filepath):
131 134
 def load_https_rules(rules_path):
132 135
     # check if directory exists
133 136
     if not isdir(rules_path):
134
-        print("[E] directory not found: '" + rules_path + "'")
137
+        logger.error("directory not found: '" + rules_path + "'")
135 138
         return
136 139
 
137 140
     # search all xml files which are stored in the https rule directory
@@ -151,7 +154,7 @@ def load_https_rules(rules_path):
151 154
         # append ruleset
152 155
         https_rules.append(ruleset)
153 156
 
154
-    print(' * {n} https-rules loaded'.format(n=len(https_rules)))
157
+    logger.info('{n} rules loaded'.format(n=len(https_rules)))
155 158
 
156 159
 
157 160
 def https_url_rewrite(result):

+ 5
- 2
searx/search.py 查看文件

@@ -29,8 +29,11 @@ from searx.engines import (
29 29
 from searx.languages import language_codes
30 30
 from searx.utils import gen_useragent
31 31
 from searx.query import Query
32
+from searx import logger
32 33
 
33 34
 
35
+logger = logger.getChild('search')
36
+
34 37
 number_of_searches = 0
35 38
 
36 39
 
@@ -42,7 +45,7 @@ def search_request_wrapper(fn, url, engine_name, **kwargs):
42 45
         engines[engine_name].stats['errors'] += 1
43 46
 
44 47
         # print engine name and specific error message
45
-        print('[E] Error with engine "{0}":\n\t{1}'.format(
48
+        logger.warning('engine crash: {0}\n\t{1}'.format(
46 49
             engine_name, str(e)))
47 50
         return
48 51
 
@@ -66,7 +69,7 @@ def threaded_requests(requests):
66 69
             remaining_time = max(0.0, timeout_limit - (time() - search_start))
67 70
             th.join(remaining_time)
68 71
             if th.isAlive():
69
-                print('engine timeout: {0}'.format(th._engine_name))
72
+                logger.warning('engine timeout: {0}'.format(th._engine_name))
70 73
 
71 74
 
72 75
 # get default reqest parameter

+ 5
- 0
searx/webapp.py 查看文件

@@ -47,8 +47,11 @@ from searx.https_rewrite import https_url_rewrite
47 47
 from searx.search import Search
48 48
 from searx.query import Query
49 49
 from searx.autocomplete import backends as autocomplete_backends
50
+from searx import logger
50 51
 
51 52
 
53
+logger = logger.getChild('webapp')
54
+
52 55
 static_path, templates_path, themes =\
53 56
     get_themes(settings['themes_path']
54 57
                if settings.get('themes_path')
@@ -68,6 +71,8 @@ app = Flask(
68 71
 
69 72
 app.secret_key = settings['server']['secret_key']
70 73
 
74
+app.logger.addHandler(logger)
75
+
71 76
 babel = Babel(app)
72 77
 
73 78
 global_favicons = []