# 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 1. Clone the repository: ```bash git clone https://git.mahdium.ir/mahdium/MQTTLogger.git cd MQTTLogger ``` 2. Install dependencies: ```bash go mod tidy ``` ## Configuration Create a `.env` file in the project root with the following environment variables: ```env 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: ```bash go run cmd/MQTTLogger/main.go ``` The application will: 1. Connect to the MQTT broker using the provided configuration 2. Subscribe to the specified topics 3. Log all received messages to `mqtt-logs.csv` in the current directory 4. 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 message - `Message`: 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 library - `go.uber.org/fx`: Dependency injection framework - `go.uber.org/zap`: Logging library - `github.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 point - `config`: Configuration loading and management - `internal/mqtt`: MQTT client implementation and connection logic - `internal/csv`: CSV file handling and logging ## License This project is licensed under the AGPL 3.0 License - see the [LICENSE](LICENSE) file for details.