使用示例
from advenced_logger import get_default_logger, exception_decorator
# Get the default logger
logger = get_default_logger()
@exception_decorator
def basic_logging_example():
"""Demonstrate basic logging functionality."""
# Basic log levels
logger.debug("Debug message - detailed information for debugging")
logger.info("Info message - general information about program execution")
logger.warning("Warning message - indicate a potential problem")
logger.error("Error message - indicate a serious problem")
logger.critical("Critical message - indicate a fatal error")
# Structured logging
logger.info(
"User logged in",
extra={
"user_id": "12345",
"ip_address": "192.168.1.1",
"login_time": "2024-01-08 10:30:00"
}
)
# Using context
with logger.contextualize(request_id="abc-123"):
logger.info("Processing request")
logger.info("Request completed", extra={"duration_ms": 150})
# Exception handling
try:
result = 1 / 0
except Exception:
logger.exception("An error occurred during calculation")
if __name__ == "__main__":
basic_logging_example()
设计多线程和多进程的示例
from advenced_logger import setup_logger, exception_decorator
import threading
import multiprocessing
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# Create custom logger with specific configuration
logger = setup_logger(
log_dir="custom_logs",
console_level="DEBUG",
file_level="INFO",
rotation="100 MB",
retention="7 days"
).get_logger()
@exception_decorator
def worker_task(task_id: int, sleep_time: float = 0.1):
"""Example worker function for demonstrating parallel logging."""
thread_name = threading.current_thread().name
process_name = multiprocessing.current_process().name
with logger.contextualize(task_id=task_id, thread=thread_name, process=process_name):
logger.info(f"Starting task {task_id}")
time.sleep(sleep_time)
# Structured logging in parallel execution
logger.info(
"Task progress",
extra={
"progress": "50%",
"memory_usage": "100MB",
"duration": sleep_time
}
)
# Simulate occasional errors
if task_id % 5 == 0:
raise ValueError(f"Simulated error in task {task_id}")
logger.info(f"Completed task {task_id}")
return task_id
@exception_decorator
def threaded_execution_example():
"""Demonstrate thread-safe logging with thread pool."""
logger.info("Starting threaded execution example")
with ThreadPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(worker_task, i) for i in range(5)]
for future in futures:
try:
result = future.result()
logger.debug(f"Thread task result: {result}")
except Exception:
logger.exception("Thread task failed")
@exception_decorator
def multiprocess_execution_example():
"""Demonstrate process-safe logging with process pool."""
logger.info("Starting multiprocess execution example")
with ProcessPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(worker_task, i) for i in range(5)]
for future in futures:
try:
result = future.result()
logger.debug(f"Process task result: {result}")
except Exception:
logger.exception("Process task failed")
if __name__ == "__main__":
# Configure logging context for the entire application
with logger.contextualize(app_name="AdvancedExample", environment="development"):
logger.info("Application starting")
# Run threading example
threaded_execution_example()
# Run multiprocessing example
multiprocess_execution_example()
logger.info("Application finished")
自定义的实例
from advanced_logger import setup_logger
import sys
def email_alert(message):
"""Mock function to simulate sending email alerts."""
print(f"Email Alert: {message}")
# Example of custom configuration with additional handlers
custom_logger = setup_logger(
log_dir="enterprise_logs",
app_log_filename="app_{time:YYYY-MM-DD}.log",
console_level="INFO",
file_level="DEBUG",
rotation="1 day",
retention="30 days",
compression="zip",
extra_handlers={
# Custom handler for critical errors
"critical_handler": {
"sink": "critical_errors.log",
"level": "CRITICAL",
"rotation": "100 MB",
"format": "{time} - {message}",
"serialize": True # JSON format
},
# Custom handler for email notifications
"email_handler": {
"sink": email_alert,
"level": "ERROR",
"format": "Alert: {message}",
"backtrace": False
}
}
).get_logger()
def demonstrate_custom_logging():
"""Show custom logging configuration features."""
# Basic logging
custom_logger.info("Application initialized with custom configuration")
# Structured logging with custom format
custom_logger.info("System status check", extra={
"memory_usage": "2.5GB",
"cpu_usage": "75%",
"disk_space": "120GB",
"active_users": 150
})
# Demonstrate error handling with email notification
try:
raise RuntimeError("Critical system error detected")
except Exception:
custom_logger.exception("System error occurred")
if __name__ == "__main__":
demonstrate_custom_logging()
Metadata
Release files for advenced-logger 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| advenced_logger-0.1.1.tar.gz | 4.9 kB | Without provenance | Details for advenced_logger-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| advenced_logger-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for advenced_logger-0.1.1-py3-none-any.whl |
Total release size: 9.8 kB
Release files / advenced_logger-0.1.1.tar.gz
| Download URL | advenced_logger-0.1.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c1b489518911661a3521f9b13ab4dee10e43298dba38007a009409888a99d789
|
|
BLAKE2b-256 checksum How to use checksums |
63e4f86fdf4d9db7287fc0277ebddfe390d2a31cc1b86f7a1d7a491f4899c892
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Release files / advenced_logger-0.1.1-py3-none-any.whl
| Download URL | advenced_logger-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06cfcb999b852ee44d898ec021035afb07a0ec0e635f8e737ae9ee33c86586ea
|
|
BLAKE2b-256 checksum How to use checksums |
af5698aded140d4bef10883ea7e51c9cf057d939267496dcf4ce4cec8974905e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Security for advenced-logger 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.