-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlog.py
More file actions
134 lines (119 loc) · 4.11 KB
/
log.py
File metadata and controls
134 lines (119 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# coding=utf-8
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
#
# 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 json
from enum import IntEnum
from typing import List, Optional
from rich import box
from rich.console import Console, Group
from rich.panel import Panel
from rich.rule import Rule
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
from rich.tree import Tree
class LogLevel(IntEnum):
OFF = -1 # No output
ERROR = 0 # Only errors
INFO = 1 # Normal output (default)
DEBUG = 2 # Detailed output
YELLOW_HEX = "#d4b702"
class AgentLogger:
def __init__(self, level: LogLevel = LogLevel.INFO):
self.level = level
# self.console = Console(width=200)
self.console = Console()
def log(self, *args, level: str | LogLevel = LogLevel.INFO, **kwargs) -> None:
"""Logs a message to the console.
Args:
level (LogLevel, optional): Defaults to LogLevel.INFO.
"""
if isinstance(level, str):
level = LogLevel[level.upper()]
if level <= self.level:
print("\n")
try:
self.console.print(*args, **kwargs)
except Exception as e:
# 打印原始内容和异常,保证日志不丢失
print("日志输出异常:", e)
print("原始日志内容:", *args)
def log_markdown(self, content: str, title: Optional[str] = None, level=LogLevel.INFO, style=YELLOW_HEX) -> None:
markdown_content = Syntax(
content,
lexer="markdown",
theme="one-dark",
word_wrap=True,
)
if title:
self.log(
Group(
Rule(
f"[bold {YELLOW_HEX}]" + title,
align="left",
style=style,
),
markdown_content,
),
level=level,
)
else:
self.log(markdown_content, level=level)
def log_code(self, title: str, content: str, level: int = LogLevel.INFO) -> None:
self.log(
Panel(
Syntax(
content,
lexer="python",
theme="monokai",
word_wrap=True,
),
title="[bold]" + title,
title_align="left",
box=box.HORIZONTALS,
),
level=level,
)
def log_rule(self, title: str, level: int = LogLevel.INFO) -> None:
self.log(
Rule(
"[bold]" + title,
characters="━",
style=YELLOW_HEX,
),
level=LogLevel.INFO,
)
def log_task(self, content: str, subtitle: str, title: Optional[str] = None, level: int = LogLevel.INFO) -> None:
self.log(
Panel(
f"\n[bold]{content}\n",
title="[bold]Step" + (f" - {title}" if title else ""),
subtitle=f"[bold {YELLOW_HEX}]{subtitle}[/]",
border_style=YELLOW_HEX,
style="yellow",
subtitle_align="left",
),
level=level,
)
def log_messages(self, messages: List) -> None:
messages_as_string = "\n".join([json.dumps(dict(message), indent=4) for message in messages])
self.log(
Syntax(
messages_as_string,
lexer="markdown",
theme="github-dark",
word_wrap=True,
)
)