Skip to content

logging

Module for logging

SpotdlFormatter ¤

Bases: Formatter

A custom logger for spotdl.

format(record) ¤

Format a log record.

Source code in spotdl/utils/logging.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def format(self, record: logging.LogRecord) -> str:
    """
    Format a log record.
    """

    result = escape(super().format(record))

    msg = result
    if record.levelno == DEBUG:
        msg = f"[blue]{result}"

    if record.levelno == MATCH:
        msg = f"[magenta]{result}"

    if record.levelno == INFO:
        msg = f"[green]{result}"

    if record.levelno == WARNING:
        msg = f"[yellow]{result}"

    if record.levelno == ERROR:
        msg = f"[red]{result}"

    if record.levelno == CRITICAL:
        msg = f"[bold red]{result}"

    return msg

SpotdlHandler ¤

Bases: RichHandler

A custom logging handler for spotdl. In this case, it's just a wrapper around the rich handler. To not highlight keywords in info messages

render_message(record, message) ¤

Render message text in to Text.

Arguments¤
  • record: logging Record.
  • message: String containing log message.
Returns¤
  • ConsoleRenderable: Renderable to display log message.
Source code in spotdl/utils/logging.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def render_message(
    self, record: logging.LogRecord, message: str
) -> "ConsoleRenderable":
    """Render message text in to Text.

    ### Arguments
    - record: logging Record.
    - message: String containing log message.

    ### Returns
    - ConsoleRenderable: Renderable to display log message.
    """

    use_markup = getattr(record, "markup", self.markup)
    message_text = Text.from_markup(message) if use_markup else Text(message)

    highlighter = getattr(record, "highlighter", self.highlighter)

    # Don't highlight info messages
    if highlighter and record.levelno != INFO:
        message_text = highlighter(message_text)

    if not hasattr(self, "keywords"):
        self.keywords = self.KEYWORDS

    # Don't highlight keywords in info messages
    if self.keywords and record.levelno != INFO:
        message_text.highlight_words(self.keywords, "logging.keyword")

    return message_text

init_logging(log_level, log_format=None) ¤

Initialize logging for spotdl.

Arguments¤
  • console: The console to use.
  • log_level: The log level to use.
Source code in spotdl/utils/logging.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def init_logging(log_level: str, log_format: Optional[str] = None):
    """
    Initialize logging for spotdl.

    ### Arguments
    - `console`: The console to use.
    - `log_level`: The log level to use.
    """

    # Don't log too much
    logging.getLogger("requests").setLevel(logging.WARNING)
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("spotipy").setLevel(logging.WARNING)
    logging.getLogger("asyncio").setLevel(logging.WARNING)
    logging.getLogger("syncedlyrics").setLevel(logging.WARNING)
    logging.getLogger("bandcamp_api").setLevel(logging.WARNING)
    logging.getLogger("beautifulsoup4").setLevel(logging.WARNING)
    logging.getLogger("pytube").setLevel(logging.ERROR)

    # Create console
    console = get_console()
    console.push_theme(THEME)

    # Add matching level loggers
    logging.addLevelName(MATCH, "MATCH")

    # Create a rich handler
    rich_handler = SpotdlHandler(
        show_time=log_level == "DEBUG",
        log_time_format="[%X]",
        omit_repeated_times=False,
        console=console,
        level=log_level,
        markup=True,
        show_path=log_level == "DEBUG",
        show_level=log_level == "DEBUG",
        rich_tracebacks=True,
    )

    msg_format = "%(message)s"
    if log_format is None:
        if log_level == "DEBUG":
            msg_format = "%(threadName)s - %(message)s"
    else:
        msg_format = log_format

    # Add rich handler to spotdl logger
    rich_handler.setFormatter(SpotdlFormatter(msg_format))

    # Create spotdl logger
    spotdl_logger = logging.getLogger("spotdl")

    # Setup spotdl logger
    spotdl_logger.setLevel(log_level)
    spotdl_logger.addHandler(rich_handler)

    # Install rich traceback handler
    install(show_locals=False, extra_lines=1, console=console)