SensorLogger
The automated data logger for sensor measurements with daily file organization.
SensorLogger
Automated data logger for sensor measurements with daily file organization.
The SensorLogger class handles periodic reading of sensor data, automatic creation of daily directories, metadata generation, and data persistence. It validates logging intervals against tag requirements and manages file operations through configurable writer strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensor
|
Sensor
|
Configured sensor instance to read measurements from. Must be properly initialized with port and communication settings. |
required |
tags
|
List[ReadTag]
|
List of ReadTag instances to log. All tags must be valid for the sensor. Each tag's metadata is used for interval validation and documentation. |
required |
interval_seconds
|
int
|
Logging interval in seconds. Must meet or exceed the minimum interval requirement of all tags (specified in tag metadata). |
required |
output_path
|
str
|
Base directory path for log file storage. Daily subdirectories will be created under this path with format YYYYMMDD (e.g., '20251024'). |
required |
writer
|
Writer
|
Strategy object for writing data files (default: CSVWriter if None). Must implement the Writer protocol with write() and get_extension() methods. |
None
|
metadata_saver
|
MetadataSaver
|
Strategy object for writing metadata files (default: JSONMetadataSaver if None). Must implement the MetadataSaver protocol with save_logger_metadata() method. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
sensor |
Sensor
|
The sensor instance being logged. |
tags |
List[ReadTag]
|
The list of tags being monitored. |
interval_seconds |
int
|
The configured logging interval. |
output_path |
Path
|
Base directory path as a Path object. |
writer |
Writer
|
The data writing strategy. |
metadata_saver |
MetadataSaver
|
The metadata writing strategy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
Basic usage with default settings:
>>> from atmospyre.sensors.implementations.co2.vaisala.gmp252 import GMP252, CO2, TEMPERATURE
>>> from atmospyre.loggers import SensorLogger
>>> sensor = GMP252(port='/dev/ttyUSB0', slave_address=1)
>>> logger = SensorLogger(
... sensor=sensor,
... tags=[CO2, TEMPERATURE],
... interval_seconds=60,
... output_path='./data'
... )
>>> logger.log() # Single measurement
>>> # Results in: ./data/20251024/data.csv
>>> # ./data/20251024/metadata.json
Notes
Directory Structure:
The logger creates the following file structure::
output_path/
├── 20251024/
│ ├── metadata.json # Created once per day
│ ├── data.csv # Appended with each log() call
│ └── errors.log # Created if errors occur
├── 20251025/
│ ├── metadata.json
│ └── data.csv
└── ...
Metadata Content:
The metadata file contains:
- Logging date and interval
- Sensor type and configuration
- Tag descriptions, units, and precision
- Minimum interval requirements
- Data file format information
Error Handling:
The log() method catches all exceptions and writes them to an error log
file in the daily directory. This prevents a single failed reading from
stopping the entire logging process. Common errors include:
- Sensor communication failures (IOError)
- Serial port disconnections
- Invalid tag readings
- Disk write failures
The logger will continue attempting to log on subsequent calls even after errors occur.
Source code in atmospyre/loggers/sensor_logger.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 | |
Functions
__init__(sensor, tags, interval_seconds, output_path, writer=None, metadata_saver=None)
Initialize sensor logger with validation.
See class docstring for parameter details.
Source code in atmospyre/loggers/sensor_logger.py
log()
Perform one logging cycle.
Executes a complete logging operation:
- Creates or accesses today's directory
- Writes metadata file if this is the first log of the day
- Reads all configured tags from the sensor
- Formats the data with timestamp
- Writes data to the daily data file
This method is designed to be called repeatedly by a scheduler at the configured interval. It handles all file operations and error logging internally.
Raises:
| Type | Description |
|---|---|
None
|
All exceptions are caught and logged to an error file. The method will not raise exceptions to prevent disrupting scheduled logging. |
Examples:
Manual single log:
>>> logger.log()
# Creates/updates:
# ./data/20251024/metadata.json (if first log today)
# ./data/20251024/data.csv (appends row)
Data Format ~~~~~~~~~~~ The data point written includes:
timestamp: ISO 8601 formatted timestamp (YYYY-MM-DDTHH:MM:SS.ffffff)- One column per tag, named by the tag's class name
- Values as returned by the sensor (typically float or int)
Example CSV output::
timestamp,CO2,TEMPERATURE
2025-10-24T14:30:00.123456,415.2,23.5
2025-10-24T14:31:00.234567,415.8,23.6
Source code in atmospyre/loggers/sensor_logger.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 | |