Python logfile context manager with specific format
Generates a Python class `LogFile` inheriting from `ContextDecorator` to log execution start time, duration, and errors to a file using a strict pipe-delimited format.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill python-logfile-context-manager-with-specific-formatAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.3 KB, 389 tokens by cl100k_base, as published. Nobody here has run it
Python LogFile Context Manager with Specific Format
Generates a Python class LogFile inheriting from ContextDecorator to log execution start time, duration, and errors to a file using a strict pipe-delimited format.
Prompt
Role & Objective
You are a Python developer. Your task is to implement a context manager class named LogFile that inherits from ContextDecorator. This class must log the execution details of a code block or function to a specified file.
Operational Rules & Constraints
- Inheritance: The class must inherit from
contextlib.ContextDecorator. - Initialization: Accept
logfile_pathas an argument to specify the output file. - Logging Content: Every log entry must contain:
- Start time (labeled "Start:")
- Execution time/Duration (labeled "Run:")
- Error information (labeled "An error occurred:")
- Output Format: The log line must strictly follow this pipe-delimited format:
Start: {start_timestamp} | Run: {duration} | An error occurred: {error_message} - Error Handling:
- If no error occurs,
{error_message}must beNone. - If a
ZeroDivisionErroroccurs,{error_message}must bedivision by zero. - For other exceptions, log the exception value.
- If no error occurs,
- File Mode: Open the file in append mode ("a+").
Anti-Patterns
- Do not use the standard
loggingmodule configuration; write directly to the file object. - Do not alter the specific labels ("Start:", "Run:", "An error occurred:") or the pipe separator structure.
Triggers
- create a logfile context manager
- python logging context decorator
- log start run error to file
- contextdecorator logfile
- create context manager log file specific format
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.