Browse Source

[enh] using the logger

Adam Tauber 10 years ago
parent
commit
299a80a1eb
4 changed files with 22 additions and 8 deletions
  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 View File

22
 from flask.ext.babel import gettext
22
 from flask.ext.babel import gettext
23
 from operator import itemgetter
23
 from operator import itemgetter
24
 from searx import settings
24
 from searx import settings
25
+from searx import logger
26
+
27
+
28
+logger = logger.getChild('engines')
25
 
29
 
26
 engine_dir = dirname(realpath(__file__))
30
 engine_dir = dirname(realpath(__file__))
27
 
31
 
81
         if engine_attr.startswith('_'):
85
         if engine_attr.startswith('_'):
82
             continue
86
             continue
83
         if getattr(engine, engine_attr) is None:
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
                   .format(engine.name, engine_attr))
89
                   .format(engine.name, engine_attr))
86
             sys.exit(1)
90
             sys.exit(1)
87
 
91
 
100
         categories['general'].append(engine)
104
         categories['general'].append(engine)
101
 
105
 
102
     if engine.shortcut:
106
     if engine.shortcut:
103
-        # TODO check duplications
104
         if engine.shortcut in engine_shortcuts:
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
                   .format(engine.shortcut))
109
                   .format(engine.shortcut))
107
             sys.exit(1)
110
             sys.exit(1)
108
         engine_shortcuts[engine.shortcut] = engine.name
111
         engine_shortcuts[engine.shortcut] = engine.name
199
 
202
 
200
 
203
 
201
 if 'engines' not in settings or not settings['engines']:
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
     exit(2)
206
     exit(2)
204
 
207
 
205
 for engine_data in settings['engines']:
208
 for engine_data in settings['engines']:

+ 5
- 2
searx/https_rewrite.py View File

20
 from lxml import etree
20
 from lxml import etree
21
 from os import listdir
21
 from os import listdir
22
 from os.path import isfile, isdir, join
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
 # https://gitweb.torproject.org/\
28
 # https://gitweb.torproject.org/\
26
 # pde/https-everywhere.git/tree/4.0:/src/chrome/content/rules
29
 # pde/https-everywhere.git/tree/4.0:/src/chrome/content/rules
27
 
30
 
131
 def load_https_rules(rules_path):
134
 def load_https_rules(rules_path):
132
     # check if directory exists
135
     # check if directory exists
133
     if not isdir(rules_path):
136
     if not isdir(rules_path):
134
-        print("[E] directory not found: '" + rules_path + "'")
137
+        logger.error("directory not found: '" + rules_path + "'")
135
         return
138
         return
136
 
139
 
137
     # search all xml files which are stored in the https rule directory
140
     # search all xml files which are stored in the https rule directory
151
         # append ruleset
154
         # append ruleset
152
         https_rules.append(ruleset)
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
 def https_url_rewrite(result):
160
 def https_url_rewrite(result):

+ 5
- 2
searx/search.py View File

29
 from searx.languages import language_codes
29
 from searx.languages import language_codes
30
 from searx.utils import gen_useragent
30
 from searx.utils import gen_useragent
31
 from searx.query import Query
31
 from searx.query import Query
32
+from searx import logger
32
 
33
 
33
 
34
 
35
+logger = logger.getChild('search')
36
+
34
 number_of_searches = 0
37
 number_of_searches = 0
35
 
38
 
36
 
39
 
42
         engines[engine_name].stats['errors'] += 1
45
         engines[engine_name].stats['errors'] += 1
43
 
46
 
44
         # print engine name and specific error message
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
             engine_name, str(e)))
49
             engine_name, str(e)))
47
         return
50
         return
48
 
51
 
66
             remaining_time = max(0.0, timeout_limit - (time() - search_start))
69
             remaining_time = max(0.0, timeout_limit - (time() - search_start))
67
             th.join(remaining_time)
70
             th.join(remaining_time)
68
             if th.isAlive():
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
 # get default reqest parameter
75
 # get default reqest parameter

+ 5
- 0
searx/webapp.py View File

47
 from searx.search import Search
47
 from searx.search import Search
48
 from searx.query import Query
48
 from searx.query import Query
49
 from searx.autocomplete import backends as autocomplete_backends
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
 static_path, templates_path, themes =\
55
 static_path, templates_path, themes =\
53
     get_themes(settings['themes_path']
56
     get_themes(settings['themes_path']
54
                if settings.get('themes_path')
57
                if settings.get('themes_path')
68
 
71
 
69
 app.secret_key = settings['server']['secret_key']
72
 app.secret_key = settings['server']['secret_key']
70
 
73
 
74
+app.logger.addHandler(logger)
75
+
71
 babel = Babel(app)
76
 babel = Babel(app)
72
 
77
 
73
 global_favicons = []
78
 global_favicons = []