Quick Start¶
Get up and running with ascii_colors
in minutes.
Option 1: Logging (Recommended)¶
This is the preferred method for structured, leveled application logs. It leverages the logging-compatible API.
Important
The key is to import the library with a familiar alias:
import ascii_colors as logging
quickstart_logging.py¶
1# Use ascii_colors with a logging-like interface
2import ascii_colors as logging # <-- Use alias for familiarity!
3from pathlib import Path
4import sys
5
6# --- Configuration ---
7# Configure logging (only runs once unless force=True)
8log_file = Path("my_app.log")
9logging.basicConfig(
10 level=logging.DEBUG, # Set the root logging level
11 # Use standard %-style formatting for console
12 format='%(asctime)s [%(levelname)-8s] %(name)s: %(message)s (%(filename)s:%(lineno)d)',
13 datefmt='%Y-%m-%d %H:%M:%S',
14 # By default, basicConfig adds a StreamHandler to stderr.
15 # Override the default stream:
16 stream=sys.stdout
17)
18
19# Add a file handler with a different format and level
20file_formatter = logging.Formatter('%(asctime)s|%(levelname)s|%(name)s|%(message)s')
21file_handler = logging.FileHandler(log_file, mode='w')
22file_handler.setLevel(logging.INFO) # Log only INFO and above to the file
23file_handler.setFormatter(file_formatter)
24logging.getLogger().addHandler(file_handler) # Add handler to the root logger
25
26# --- Usage ---
27# Get a logger instance (like standard logging)
28logger = logging.getLogger("MyApp")
29
30# Log messages at different levels
31logger.debug("Debug information: connection established.") # To stdout only
32logger.info("Application starting up...") # To stdout and file
33logger.warning("Config setting 'backup_enabled' not found.") # To stdout and file
34
35user = "Alice"
36logger.info("User '%s' logged in.", user) # Supports %-formatting arguments
37
38try:
39 result = 10 / 0
40except ZeroDivisionError:
41 # logger.error() with exc_info=True logs the error and traceback
42 logger.error("Critical calculation failed!", exc_info=True)
43 # logger.exception() is a shorthand for error() inside except blocks
44 # logger.exception("Critical calculation failed!")
45
46logger.critical("System integrity compromised!") # To stdout and file
47
48print(f"\nCheck console output (stdout) and the log file '{log_file}'")
Option 2: Direct Printing¶
Use these methods for simple, immediate styled output directly to the console. They do not use the logging system (handlers, levels, formatters).
quickstart_direct_print.py¶
1from ascii_colors import ASCIIColors
2
3# Direct print methods bypass the logging system
4ASCIIColors.red("This is an urgent error message.")
5ASCIIColors.green("Operation completed successfully!")
6ASCIIColors.yellow("Warning: Disk space low.")
7
8# Combine with styles and specific colors
9ASCIIColors.bold("This is important!", color=ASCIIColors.color_bright_white)
10ASCIIColors.underline("Underlined text.", color=ASCIIColors.color_cyan)
11ASCIIColors.italic("Italic blue text.", color=ASCIIColors.color_blue)
12
13# Use background colors
14ASCIIColors.print_with_bg(
15 " Black text on Orange background ",
16 color=ASCIIColors.color_black,
17 background=ASCIIColors.bg_orange
18)
19
20# Combine foreground, background, and style
21ASCIIColors.print(
22 " Bold Red text on Yellow background ",
23 color=ASCIIColors.color_red,
24 style=ASCIIColors.style_bold,
25 background=ASCIIColors.bg_yellow
26)