.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring, cyclic-import
3
4import sys
5import os
6from os.path import dirname, abspath
7
8import logging
9
12from searx.settings_defaults import settings_set_defaults
13
14
15# Debug
16LOG_FORMAT_DEBUG = '%(levelname)-7s %(name)-30.30s: %(message)s'
17
18# Production
19LOG_FORMAT_PROD = '%(asctime)-15s %(levelname)s:%(name)s: %(message)s'
20LOG_LEVEL_PROD = logging.WARNING
21
22searx_dir = abspath(dirname(__file__))
23searx_parent_dir = abspath(dirname(dirname(__file__)))
24settings, settings_load_message = searx.settings_loader.load_settings()
25
26if settings is not None:
27 settings = settings_set_defaults(settings)
28
29_unset = object()
30
31
32def get_setting(name, default=_unset):
33 """Returns the value to which ``name`` point. If there is no such name in the
34 settings and the ``default`` is unset, a :py:obj:`KeyError` is raised.
35
36 """
37 value = settings
38 for a in name.split('.'):
39 if isinstance(value, dict):
40 value = value.get(a, _unset)
41 else:
42 value = _unset
43
44 if value is _unset:
45 if default is _unset:
46 raise KeyError(name)
47 value = default
48 break
49
50 return value
51
52
54 if os.getenv('TERM') in ('dumb', 'unknown'):
55 return False
56 return sys.stdout.isatty()
57
58
60 try:
61 import coloredlogs # pylint: disable=import-outside-toplevel
62 except ImportError:
63 coloredlogs = None
64
65 log_level = os.environ.get('SEARXNG_DEBUG_LOG_LEVEL', 'DEBUG')
66 if coloredlogs and is_color_terminal():
67 level_styles = {
68 'spam': {'color': 'green', 'faint': True},
69 'debug': {},
70 'notice': {'color': 'magenta'},
71 'success': {'bold': True, 'color': 'green'},
72 'info': {'bold': True, 'color': 'cyan'},
73 'warning': {'color': 'yellow'},
74 'error': {'color': 'red'},
75 'critical': {'bold': True, 'color': 'red'},
76 }
77 field_styles = {
78 'asctime': {'color': 'green'},
79 'hostname': {'color': 'magenta'},
80 'levelname': {'color': 8},
81 'name': {'color': 8},
82 'programname': {'color': 'cyan'},
83 'username': {'color': 'yellow'},
84 }
85 coloredlogs.install(level=log_level, level_styles=level_styles, field_styles=field_styles, fmt=LOG_FORMAT_DEBUG)
86 else:
87 logging.basicConfig(level=logging.getLevelName(log_level), format=LOG_FORMAT_DEBUG)
88
89
90searx_debug = settings['general']['debug']
91if searx_debug:
93else:
94 logging.basicConfig(level=LOG_LEVEL_PROD, format=LOG_FORMAT_PROD)
95 logging.root.setLevel(level=LOG_LEVEL_PROD)
96 logging.getLogger('werkzeug').setLevel(level=LOG_LEVEL_PROD)
97logger = logging.getLogger('searx')
98logger.info(settings_load_message)
99
100# log max_request_timeout
101max_request_timeout = settings['outgoing']['max_request_timeout']
102if max_request_timeout is None:
103 logger.info('max_request_timeout=%s', repr(max_request_timeout))
104else:
105 logger.info('max_request_timeout=%i second(s)', max_request_timeout)
106
107if settings['server']['public_instance']:
108 logger.warning(
109 "Be aware you have activated features intended only for public instances. "
110 "This force the usage of the limiter and link_token / "
111 "see https://docs.searxng.org/admin/searx.limiter.html"
112 )
tuple[dict, str] load_settings(load_user_settings=True)
get_setting(name, default=_unset)
Definition __init__.py:32
logging_config_debug()
Definition __init__.py:59
is_color_terminal()
Definition __init__.py:53