logkiss Core Module¶
The logkiss core module contains basic classes and functions for colorful log output.
logkiss.logkiss
¶
Core module of logkiss.
Copyright (c) 2025 Taka Suzuki SPDX-License-Identifier: MIT See LICENSE for details.
This module provides the core functionality of logkiss, including logging setup and configuration management.
Classes¶
ColorConfig
dataclass
¶
ColorManager
¶
Class to manage color settings
Source code in logkiss/logkiss.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
Functions¶
__init__(config_path=None)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Optional[Union[str, Path]]
|
Path to color configuration file |
None
|
apply_color(text, config)
¶
Apply color settings to text
Source code in logkiss/logkiss.py
colorize_filename(filename)
¶
colorize_level(levelname, levelno=None)
¶
Colorize log level name
Source code in logkiss/logkiss.py
colorize_message(message, level)
¶
colorize_timestamp(timestamp)
¶
get_element_color(element)
¶
get_level_color(level)
¶
Get color settings for a log level
Source code in logkiss/logkiss.py
get_message_color(level)
¶
Get color settings for a log message
Source code in logkiss/logkiss.py
ColoredFormatter
¶
Bases: Formatter
Formatter that applies colors to log messages based on their level.
This formatter extends the standard logging.Formatter to add color to log messages based on their level. Colors can be customized through a configuration file.
To disable colors, set use_color=False when creating the formatter: formatter = ColoredFormatter(use_color=False)
Note
When using KissConsoleHandler, a ColoredFormatter is automatically created with use_color set based on the output stream. To disable colors with KissConsoleHandler, you need to create a ColoredFormatter with use_color=False and set it explicitly: handler = KissConsoleHandler() formatter = ColoredFormatter(use_color=False) handler.setFormatter(formatter)
Environment Variables
The following environment variables can be used to control coloring: - LOGKISS_DISABLE_COLOR: Disable colors (values: 1, true, yes) - NO_COLOR: Industry standard to disable colors (any value) These environment variables override the use_color parameter.
The following environment variable can be used to control level name formatting: - LOGKISS_LEVEL_FORMAT: Specify the length of level names (value: integer, default: 5) For example, LOGKISS_LEVEL_FORMAT=5 will adjust all level names to be 5 characters. WARNING is specially shortened to "WARN". Level names longer than the specified length will be truncated, and shorter ones will be padded with spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
Optional[str]
|
Format string for log messages. Default is '%(asctime)s %(levelname)s | %(filename)s: %(lineno)d | %(message)s' |
None
|
datefmt
|
Optional[str]
|
Date format string. Default is None (ISO8601 format). |
None
|
style
|
str
|
Format style ('%', '{', '$'). Default is '%'. |
'%'
|
validate
|
bool
|
Whether to validate format string. Default is True. |
True
|
color_config
|
Optional[Union[str, Path]]
|
Path to color configuration file. Default is None. |
None
|
use_color
|
bool
|
Whether to apply colors to log messages. Default is True. |
True
|
Source code in logkiss/logkiss.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
Functions¶
__init__(fmt=None, datefmt=None, style='%', validate=True, color_config=None, use_color=True, format=None)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
Optional[str]
|
Format string |
None
|
datefmt
|
Optional[str]
|
Date format string |
None
|
style
|
str
|
Format style ('%', '{', '$') |
'%'
|
validate
|
bool
|
Validate format string |
True
|
color_config
|
Optional[Union[str, Path]]
|
Path to color configuration file |
None
|
use_color
|
bool
|
Apply colors to log messages |
True
|
Source code in logkiss/logkiss.py
format(record)
¶
Format log record with colors
Source code in logkiss/logkiss.py
Colors
¶
ANSI escape sequences
Source code in logkiss/logkiss.py
KissConsoleHandler
¶
Bases: StreamHandler
Handler that outputs colored log messages to the console.
This handler extends the standard logging.StreamHandler to add color to log messages based on their level. Colors can be customized through a configuration file.
By default, colors are enabled when outputting to sys.stderr or sys.stdout. To disable colors, you need to create a ColoredFormatter with use_color=False and set it explicitly: handler = KissConsoleHandler() formatter = ColoredFormatter(use_color=False) handler.setFormatter(formatter)
Environment Variables
The following environment variables can be used to control coloring: - LOGKISS_DISABLE_COLOR: Disable colors (values: 1, true, yes) - NO_COLOR: Industry standard to disable colors (any value) These environment variables override the use_color parameter of the formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Optional[TextIO]
|
Output stream. Default is sys.stderr. |
None
|
color_config
|
Optional[Union[str, Path]]
|
Path to color configuration file. Default is None. |
None
|
Source code in logkiss/logkiss.py
Functions¶
__init__(stream=None, color_config=None)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Optional[TextIO]
|
Output stream |
None
|
color_config
|
Optional[Union[str, Path]]
|
Path to color configuration file |
None
|
Source code in logkiss/logkiss.py
emit(record)
¶
Output log record
Source code in logkiss/logkiss.py
KissLogger
¶
Bases: Logger
Logger that uses colored output by default
Source code in logkiss/logkiss.py
Functions¶
__init__(name)
¶
makeRecord(name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)
¶
Create a LogRecord with the given arguments
Source code in logkiss/logkiss.py
reload_config()
¶
Reload configuration from the original source.
This method reloads the logger configuration from the original YAML file if it was configured using setup_from_yaml, or from environment variables if it was configured using setup_from_env.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the logger was not configured using setup_from_yaml or setup_from_env. |
Source code in logkiss/logkiss.py
setLevel(level)
¶
Set the logging level for both logger and handlers
PathShortenerFilter
¶
Bases: Filter
Filter to shorten paths in log messages
Shortens long paths to ".../
Controlled by environment variable LOGKISS_PATH_SHORTEN: - 0 or invalid value: Disable path shortening - Positive integer: Show last n components
Source code in logkiss/logkiss.py
Functions¶
setup_from_env()
¶
Set up logging configuration from environment variables.
Environment variables
LOGKISS_LEVEL: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) LOGKISS_FORMAT: Log format string LOGKISS_DISABLE_COLOR: Disable colored output if 'true'
Returns:
| Type | Description |
|---|---|
Logger
|
Configured logger instance |
Source code in logkiss/logkiss.py
setup_from_yaml(config_path)
¶
Set up logging configuration from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Union[str, Path]
|
Path to YAML configuration file |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
Configured logger instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If config file does not exist |
YAMLError
|
If config file is invalid YAML |
Source code in logkiss/logkiss.py
use_console_handler(logger=None)
¶
Configure the logger to use a standard StreamHandler instead of KissConsoleHandler.
This function removes any existing KissConsoleHandler from the specified logger and adds a standard StreamHandler with a basic formatter. This is useful when you want to disable the colored output and use a simple console handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Optional[Logger]
|
Logger to configure. Default is None (root logger). |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
import logkiss as logging logger = logging.getLogger(name) logging.use_console_handler(logger) logger.info('This message will be displayed without color')