6acdcb742932ed2e98152eb2bdc52003505fe275
				
			
			
		
	MQTT Logger
A Go application that subscribes to MQTT topics and logs all messages to a CSV file for analysis and monitoring.
Features
- Subscribe to one or multiple MQTT topics
 - Log all MQTT messages to a CSV file with timestamp, topic, and message content
 - Support for MQTT authentication (username/password)
 - Configurable logging levels
 - Automatic CSV header initialization
 - Connection status monitoring and logging
 
Prerequisites
- Go 1.25.1 or higher
 - Access to an MQTT broker
 
Installation
- 
Clone the repository:
git clone https://git.mahdium.ir/mahdium/MQTTLogger.git cd MQTTLogger - 
Install dependencies:
go mod tidy 
Configuration
Create a .env file in the project root with the following environment variables:
MQTT_URI=mqtt://your-mqtt-broker:1883
MQTT_USERNAME=your-username  # Optional
MQTT_PASSWORD=your-password  # Optional
MQTT_TOPICS=#                # Optional, defaults to "#" (all topics)
LOG_LEVEL=info              # Optional, defaults to "info"
Usage
Run the application:
go run cmd/MQTTLogger/main.go
The application will:
- Connect to the MQTT broker using the provided configuration
 - Subscribe to the specified topics
 - Log all received messages to 
mqtt-logs.csvin the current directory - Monitor and log connection status
 
Output
The application creates a CSV file named mqtt-logs.csv with the following columns:
Date (UTC): Timestamp when the message was received (in UTC)Topic: MQTT topic of the messageMessage: Content of the MQTT message
Example Output
Date (UTC),Topic,Message
2025-09-29 10:30:45,home/sensor/temperature,{"value": 23.5}
2025-09-29 10:30:46,home/sensor/humidity,{"value": 45.2}
Dependencies
github.com/eclipse/paho.mqtt.golang: MQTT client librarygo.uber.org/fx: Dependency injection frameworkgo.uber.org/zap: Logging librarygithub.com/joho/godotenv: Environment variable loading
Architecture
The application uses the Uber FX framework for dependency injection and is organized into the following packages:
cmd/MQTTLogger: Main application entry pointconfig: Configuration loading and managementinternal/mqtt: MQTT client implementation and connection logicinternal/csv: CSV file handling and logging
License
This project is licensed under the AGPL 3.0 License - see the LICENSE file for details.
					Languages
				
				
								
								
									Go
								
								100%