logging.py 3.8 KB
Newer Older
luopl's avatar
luopl committed
1
2
3
4
# Copyright 2024 Optuna, HuggingFace Inc. and the LlamaFactory team.
#
# This code is inspired by the HuggingFace's transformers library.
# https://github.com/huggingface/transformers/blob/v4.40.0/src/transformers/utils/logging.py
chenych's avatar
chenych committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import sys
luopl's avatar
luopl committed
21
import threading
chenych's avatar
chenych committed
22
from concurrent.futures import ThreadPoolExecutor
luopl's avatar
luopl committed
23
from typing import Optional
chenych's avatar
chenych committed
24
25
26
27

from .constants import RUNNING_LOG


luopl's avatar
luopl committed
28
29
30
31
32
_thread_lock = threading.RLock()
_default_handler: Optional["logging.Handler"] = None
_default_log_level: "logging._Level" = logging.INFO


chenych's avatar
chenych committed
33
34
class LoggerHandler(logging.Handler):
    r"""
luopl's avatar
luopl committed
35
    Redirects the logging output to the logging file for LLaMA Board.
chenych's avatar
chenych committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    """

    def __init__(self, output_dir: str) -> None:
        super().__init__()
        formatter = logging.Formatter(
            fmt="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S"
        )
        self.setLevel(logging.INFO)
        self.setFormatter(formatter)

        os.makedirs(output_dir, exist_ok=True)
        self.running_log = os.path.join(output_dir, RUNNING_LOG)
        if os.path.exists(self.running_log):
            os.remove(self.running_log)

        self.thread_pool = ThreadPoolExecutor(max_workers=1)

    def _write_log(self, log_entry: str) -> None:
        with open(self.running_log, "a", encoding="utf-8") as f:
            f.write(log_entry + "\n\n")

    def emit(self, record) -> None:
        if record.name == "httpx":
            return

        log_entry = self.format(record)
        self.thread_pool.submit(self._write_log, log_entry)

    def close(self) -> None:
        self.thread_pool.shutdown(wait=True)
        return super().close()


luopl's avatar
luopl committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def _get_default_logging_level() -> "logging._Level":
    r"""
    Returns the default logging level.
    """
    env_level_str = os.environ.get("LLAMAFACTORY_VERBOSITY", None)
    if env_level_str:
        if env_level_str.upper() in logging._nameToLevel:
            return logging._nameToLevel[env_level_str.upper()]
        else:
            raise ValueError("Unknown logging level: {}.".format(env_level_str))

    return _default_log_level


def _get_library_name() -> str:
    return __name__.split(".")[0]


def _get_library_root_logger() -> "logging.Logger":
    return logging.getLogger(_get_library_name())


def _configure_library_root_logger() -> None:
chenych's avatar
chenych committed
92
    r"""
luopl's avatar
luopl committed
93
    Configures root logger using a stdout stream handler with an explicit format.
chenych's avatar
chenych committed
94
    """
luopl's avatar
luopl committed
95
    global _default_handler
chenych's avatar
chenych committed
96

luopl's avatar
luopl committed
97
98
99
    with _thread_lock:
        if _default_handler:
            return
chenych's avatar
chenych committed
100

luopl's avatar
luopl committed
101
102
103
104
105
106
107
108
109
110
        formatter = logging.Formatter(
            fmt="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
            datefmt="%m/%d/%Y %H:%M:%S",
        )
        _default_handler = logging.StreamHandler(sys.stdout)
        _default_handler.setFormatter(formatter)
        library_root_logger = _get_library_root_logger()
        library_root_logger.addHandler(_default_handler)
        library_root_logger.setLevel(_get_default_logging_level())
        library_root_logger.propagate = False
chenych's avatar
chenych committed
111
112


luopl's avatar
luopl committed
113
def get_logger(name: Optional[str] = None) -> "logging.Logger":
chenych's avatar
chenych committed
114
    r"""
luopl's avatar
luopl committed
115
    Returns a logger with the specified name. It it not supposed to be accessed externally.
chenych's avatar
chenych committed
116
    """
luopl's avatar
luopl committed
117
118
119
120
121
    if name is None:
        name = _get_library_name()

    _configure_library_root_logger()
    return logging.getLogger(name)