Skip to content

Commit 02188a8

Browse files
authored
Update readme.md
More better
1 parent 7486b8b commit 02188a8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

readme.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Apache Access Log Parser to MySQL
23

34
This Python script parses Apache access logs and inserts the log entries into a specified MySQL database table. It handles batch inserts for efficiency and supports datetime conversion to the MySQL `DATETIME` format.
@@ -24,3 +25,42 @@ This Python script parses Apache access logs and inserts the log entries into a
2425

2526
```sh
2627
python insert_logs.py <access_log_file> <mysql_host> <mysql_user> <mysql_password> <db_name> <table_name>
28+
```
29+
30+
### Arguments
31+
32+
- `<access_log_file>`: Path to the Apache access log file.
33+
- `<mysql_host>`: MySQL host (e.g., `localhost`).
34+
- `<mysql_user>`: MySQL username.
35+
- `<mysql_password>`: MySQL password.
36+
- `<db_name>`: Name of the MySQL database.
37+
- `<table_name>`: Name of the table to insert the log entries.
38+
39+
## Script Details
40+
41+
### Functions
42+
43+
- `parse_log_line(line)`: Parses a single line of the Apache access log.
44+
- `convert_to_mysql_datetime(apache_datetime)`: Converts Apache log datetime to MySQL `DATETIME` format.
45+
- `insert_logs(cursor, logs, table_name)`: Inserts logs into the specified table in batches.
46+
- `create_table(cursor, table_name)`: Creates the specified table if it does not already exist.
47+
48+
### Flow
49+
50+
1. **Database Connection**: Establishes a connection to the specified MySQL database.
51+
2. **Table Creation**: Creates the specified table if it does not exist.
52+
3. **Log Parsing**: Reads the access log file line by line, parses each line, and converts the datetime format.
53+
4. **Batch Insert**: Inserts log entries into the table in batches for efficiency.
54+
5. **Commit and Close**: Commits the transaction and closes the database connection.
55+
56+
## Error Handling
57+
58+
The script includes basic error handling to catch and display database connection errors, table creation errors, and insertion errors.
59+
60+
## License
61+
62+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
63+
64+
## Contributing
65+
66+
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

0 commit comments

Comments
 (0)