log
Logging Configuration and Management
A comprehensive logging utility module that provides flexible and configurable logging setup for Python applications, supporting both console and file-based logging with color formatting options.
Environment Variable
ROMI_APP_LOGGER is an optional environment variable that can be used to override the default logger name.
When ROMI_APP_LOGGER is defined, the get_logger function will use its value as the logger name instead
of the module‑derived name. This allows a single logger configuration to be shared across multiple components
of the ROMI application.
Key Features
- Configurable log levels with sensible defaults
- Console logging with optional color formatting
- File-based logging with automatic file handling
- Dummy logger for testing/development
- Centralized logging configuration management
- Custom formatting options for log messages
- Dynamic log filename generation
Usage Examples
Basic usageLink
from plantdb.log import get_logger logger = get_logger(name) logger.info("Application started") INFO [main] Application started logger.error("An error occurred") ERROR [main] An error occurred
With custom configurationLink
logger = get_logger(name, log_level="DEBUG") logger.debug("Debugging information") DEBUG [main] Debugging information
With App loggerLink
import os logger = get_logger('MyApp', log_level="INFO") os.environ.setdefault('ROMI_APP_LOGGER', 'fsdb_rest_api')
Can be called by another module, the environment variable will override logger nameLink
another_logger = get_logger('MyModule') another_logger.info("Debugging information") INFO [fsdb_rest_api] Debugging information
get_console_handler
Link
get_console_handler()
Creates and configures a console handler for logging that outputs to the standard output stream.
This handler uses a specific formatter for colored log messages.
Returns:
| Type | Description |
|---|---|
StreamHandler
|
The configured console logging handler with a colored formatter. |
Source code in plantdb/commons/log.py
112 113 114 115 116 117 118 119 120 121 122 123 124 | |
get_dummy_logger
Link
get_dummy_logger(logger_name, log_level=DEFAULT_LOG_LEVEL)
Creates and configures a dummy logger with a console handler.
This function generates a logger instance associated with the specified name and log level. The logger is configured with a console handler to allow output of log statements to the console. It is typically used for basic logging setups where no file-based or external configurations are required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
A name to use for the logger. |
required |
|
int or str
|
A logging level to set for the logger. Defaults to |
DEFAULT_LOG_LEVEL
|
Returns:
| Type | Description |
|---|---|
Logger
|
The configured logger instance with a console handler attached. |
Source code in plantdb/commons/log.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
get_file_handler
Link
get_file_handler(log_file)
Creates and configures a file handler for logging.
This function initializes a logging file handler, sets its level, and applies a predefined formatter to it. The file handler writes log messages to the specified file in write mode. The log level determines the severity of messages that are captured by the handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
A path to the log file where log messages will be written. |
required |
Returns:
| Type | Description |
|---|---|
FileHandler
|
The configured logging file handler for capturing log messages. |
Source code in plantdb/commons/log.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
get_logger
Link
get_logger(logger_name, log_file=None, log_level=DEFAULT_LOG_LEVEL)
Get a logger with a specific name and log level for console output.
This function retrieves an existing logger instance with the specified name, or creates a new one if none exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
A name to use for the logger. Typically derived from the module or component that generates the logs. |
required |
|
str or Path
|
A path to the file where log messages should be written.
Defaults to |
None
|
|
int or str
|
A logging level to set for the logger. Defaults to |
DEFAULT_LOG_LEVEL
|
Returns:
| Type | Description |
|---|---|
Logger
|
The configured logger instance ready to log messages with the specified settings. |
Source code in plantdb/commons/log.py
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 | |