.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 SCHEMA, apply_schema
13
14# Debug
15LOG_FORMAT_DEBUG = '%(levelname)-7s %(name)-30.30s: %(message)s'
16
17# Production
18LOG_FORMAT_PROD = '%(asctime)-15s %(levelname)s:%(name)s: %(message)s'
19LOG_LEVEL_PROD = logging.WARNING
20
21searx_dir = abspath(dirname(__file__))
22searx_parent_dir = abspath(dirname(dirname(__file__)))
23
24settings = {}
25searx_debug = False
26logger = logging.getLogger('searx')
27
28_unset = object()
29
30
32 """Initialize global ``settings`` and ``searx_debug`` variables and
33 ``logger`` from ``SEARXNG_SETTINGS_PATH``.
34 """
35
36 global settings, searx_debug # pylint: disable=global-variable-not-assigned
37
38 cfg, msg = searx.settings_loader.load_settings(load_user_settings=True)
39 cfg = cfg or {}
40 apply_schema(cfg, SCHEMA, [])
41
42 settings.clear()
43 settings.update(cfg)
44
45 searx_debug = settings['general']['debug']
46 if searx_debug:
48 else:
49 logging.basicConfig(level=LOG_LEVEL_PROD, format=LOG_FORMAT_PROD)
50 logging.root.setLevel(level=LOG_LEVEL_PROD)
51 logging.getLogger('werkzeug').setLevel(level=LOG_LEVEL_PROD)
52 logger.info(msg)
53
54 # log max_request_timeout
55 max_request_timeout = settings['outgoing']['max_request_timeout']
56 if max_request_timeout is None:
57 logger.info('max_request_timeout=%s', repr(max_request_timeout))
58 else:
59 logger.info('max_request_timeout=%i second(s)', max_request_timeout)
60
61 if settings['server']['public_instance']:
62 logger.warning(
63 "Be aware you have activated features intended only for public instances. "
64 "This force the usage of the limiter and link_token / "
65 "see https://docs.searxng.org/admin/searx.limiter.html"
66 )
67
68
69def get_setting(name, default=_unset):
70 """Returns the value to which ``name`` point. If there is no such name in the
71 settings and the ``default`` is unset, a :py:obj:`KeyError` is raised.
72
73 """
74 value = settings
75 for a in name.split('.'):
76 if isinstance(value, dict):
77 value = value.get(a, _unset)
78 else:
79 value = _unset
80
81 if value is _unset:
82 if default is _unset:
83 raise KeyError(name)
84 value = default
85 break
86
87 return value
88
89
91 if os.getenv('TERM') in ('dumb', 'unknown'):
92 return False
93 return sys.stdout.isatty()
94
95
97 try:
98 import coloredlogs # pylint: disable=import-outside-toplevel
99 except ImportError:
100 coloredlogs = None
101
102 log_level = os.environ.get('SEARXNG_DEBUG_LOG_LEVEL', 'DEBUG')
103 if coloredlogs and _is_color_terminal():
104 level_styles = {
105 'spam': {'color': 'green', 'faint': True},
106 'debug': {},
107 'notice': {'color': 'magenta'},
108 'success': {'bold': True, 'color': 'green'},
109 'info': {'bold': True, 'color': 'cyan'},
110 'warning': {'color': 'yellow'},
111 'error': {'color': 'red'},
112 'critical': {'bold': True, 'color': 'red'},
113 }
114 field_styles = {
115 'asctime': {'color': 'green'},
116 'hostname': {'color': 'magenta'},
117 'levelname': {'color': 8},
118 'name': {'color': 8},
119 'programname': {'color': 'cyan'},
120 'username': {'color': 'yellow'},
121 }
122 coloredlogs.install(level=log_level, level_styles=level_styles, field_styles=field_styles, fmt=LOG_FORMAT_DEBUG)
123 else:
124 logging.basicConfig(level=logging.getLevelName(log_level), format=LOG_FORMAT_DEBUG)
125
126
tuple[dict, str] load_settings(load_user_settings=True)
_logging_config_debug()
Definition __init__.py:96
_is_color_terminal()
Definition __init__.py:90
init_settings()
Definition __init__.py:31
get_setting(name, default=_unset)
Definition __init__.py:69