Advanced Skills Logging and Debugging
Beginner debugging relies on print; advanced debugging relies on systematic logging mechanisms.
This article introduces how to establish structured logging for Skill scripts and advanced techniques for locating problems in complex execution flows.
Replacing print with logging
print is suitable for quick debugging, but in formal Skills you should use the Python standard library's logging module instead.
The advantage of logging is that you can control output levels—output DEBUG information during development and only WARNING or above in production.
Example
File path: scripts/logger_setup.py
import
logging
import
sys
from
datetime
import
datetime
def
get_logger
(
name:
str
,
level:
str
=
"INFO"
)
logging
.
Logger
:
"""
Create a structured logger
Parameters:
name: Logger name, usually the module name (name)
level: Log level, DEBUG / INFO / WARNING / ERROR
"""
logger
=
logging
.
getLogger
(
name
)
logger.
setLevel
(
getattr
(
logging
,
level.
upper
(
)
,
logging
.
INFO
)
)
Avoid adding handlers repeatedly
if
logger.
handlers
:
return
logger
Console output: human-readable format
console
=
logging
.
StreamHandler
(
sys
.
stdout
)
console.
setFormatter
(
logging
.
Formatter
(
"[%(asctime)s] [%(levelname)s] %(name)s - %(message)s"
,
datefmt
=
"%H:%M:%S"
)
)
logger.
addHandler
(
console
)
File output: write to a log file (optional)
log_file
=
f
"/home/claude/skill_{datetime.now().strftime('%Y%m%d')}.log"
file_handler
=
logging
.
FileHandler
(
log_file
,
encoding
=
"utf-8"
)
file_handler.
setFormatter
(
logging
.
Formatter
(
"%(asctime)s [%(levelname)s] %(name)s:%(lineno)d - %(message)s"
)
)
logger.
addHandler
(
file_handler
)
return
logger
Usage examples
if
name
==
"main"
:
log
=
get_logger
(
name
,
level
=
"DEBUG"
)
log.
debug
(
"Debug info: file path = /mnt/user-data/uploads/runoob.csv"
)
log.
info
(
"Start processing file"
)
log.
warning
(
"File size exceeds 50MB, processing may be slow"
)
log.
error
(
"File read failed: FileNotFoundError"
)
[10:23:01] [DEBUG] __main__ - 调试信息:文件路径 = /mnt/user-data/uploads/runoob.csv
[10:23:01] [INFO] __main__ - 开始处理文件
[10:23:01] [WARNING] __main__ - 文件大小超过 50MB,处理可能较慢
[10:23:01] [ERROR] __main__ - 文件读取失败:FileNotFoundError
Execution Time Tracking
When a Skill executes slowly, you need to find the time-consuming bottleneck steps.
Example
File path: scripts/timer.py
import
time
import
functools
import
logging
log
=
logging
.
getLogger
(
name
)
def
timeit
(
func
)
:
"""Decorator: automatically record function execution time"""
@
functools.
wraps
(
func
)
def
wrapper
(
*args
,
**kwargs
)
:
start
=
time
.
perf_counter
(
)
result
=
func
(
*args
,
**kwargs
)
elapsed
=
time
.
perf_counter
(
)
- start
log.
info
(
f
"{func.name} took {elapsed:.3f} seconds"
)
return
result
return
wrapper
Usage: add the @timeit decorator to functions that need timing
@
timeit
def
load_csv
(
file_path:
str
)
:
import
pandas
as
pd
return
pd.
read_csv
(
file_path
)
@
timeit
def
calculate_stats
(
df
)
:
return
df.
describe
(
)
You can also use a context manager for manual timing
class
Timer:
def
init
(
self
,
label:
str
)
:
self
.
label
=
label
def
enter
(
self
)
:
self
.
start
=
time
.
perf_counter
(
)
return
self
def
exit
(
self
,
*args
)
:
elapsed
=
time
.
perf_counter
(
)
self
.
start
log.
info
(
f
"{self.label} took {elapsed:.3f} seconds"
)
Use the Timer context manager
if
name
==
"main"
:
with
Timer
(
"Read and clean data"
)
:
import
pandas
as
pd
df
=
pd.
read_csv
(
"/mnt/user-data/uploads/runoob_data.csv"
)
df
=
df.
dropna
(
)
[10:23:05] [INFO] load_csv 耗时 0.342 秒
[10:23:05] [INFO] calculate_stats 耗时 0.018 秒
[10:23:05] [INFO] 读取并清洗数据 耗时 0.361 秒
Structured Logging: Outputting JSON Format
When logs need to be parsed by programs (rather than read by humans), JSON format is more suitable.
Example
File path: scripts/json_logger.py
import
json
import
sys
from
datetime
import
datetime
def
log_event
(
level:
str
,
event:
str
,
**context
)
:
"""
Output structured JSON logs
Parameters:
level: Log level (info / warning / error)
event: Event name
context: Additional context key-value pairs
"""
entry
=
{
"timestamp"
:
datetime
.
now
(
)
.
isoformat
(
)
,
"level"
: level
,
"event"
: event
,
**context
}
Use stderr to output logs to avoid mixing with the script's normal output
(
json.
dumps
(
entry
,
ensure_ascii
=
False
)
,
file
=
sys
.
stderr
)
Usage examples
log_event
(
"info"
,
"file_loaded"
,
file
=
"/mnt/user-data/uploads/runoob.csv"
,
rows
=
1024
)
log_event
(
"warning"
,
"null_detected"
,
column
=
"score"
,
count
=
12
)
log_event
(
"error"
,
"parse_failed"
,
reason
=
"Encoding is not UTF-8"
)
{"timestamp": "2026-05-18T10:23:05", "level": "info", "event": "file_loaded", "file": "/mnt/user-data/uploads/runoob.csv", "rows": 1024}
{"timestamp": "2026-05-18T10:23:05", "level": "warning", "event": "null_detected", "column": "score", "count": 12}
{"timestamp": "2026-05-18T10:23:05", "level": "error", "event": "parse_failed", "reason": "编码不是 UTF-8"}
Debugging SKILL.md Instruction Execution
When Claude does not execute according to SKILL.md's instructions, you can locate the problem by inserting "checkpoints" in SKILL.md.
## Debug checkpoint (delete before release)
Before executing each step, first output a line of status confirmation in the following format:
`[STEP N] 开始:{步骤名称},输入:{关键参数}`
示例:
`[STEP 1] 开始:读取文件,输入:/mnt/user-data/uploads/runoob.csv`
`[STEP 2] 开始:数据清洗,输入:1024 行`
这样可以在出错时快速定位到哪个步骤失败。
Debugging checkpoints are temporary. After debugging is complete, be sure to remove these instructions from SKILL.md; otherwise users will see extra debugging output during formal use.
Quick Reference for Common Debugging Scenarios
| Scenario | Debugging method |
|---|---|
| Skill not triggered | Check the description, test with complex prompts, and run run_loop.py for optimization |
| Script reports ImportError | Run pip install and check whether package names match import names |
| File path error | First run ls /mnt/user-data/uploads/ to confirm the file name |
| Garbled output | Explicitly specify encoding="utf-8" when reading files |
| Steps executed in the wrong order | Use a numbered list in SKILL.md to clarify the step order |
| Empty result | Add print statements at key points in the script to confirm at which step the data becomes empty |