LogGuard is a contract testing tool for validating the log codes generated by your application. It uses a YAML contract, a plain, human-readable file, that describes all the log codes your application should generate, including their severity level and a brief description.
Before running LogGuard, make sure you have set up the LOG_CODES_YAML
environment variable pointing to the path of your log_codes.yaml
file.
You can set this up in Unix systems using the export
command:
export LOG_CODES_YAML=/path/to/your/log_codes.yaml
Or in Windows using the set
command:
set LOG_CODES_YAML=/path/to/your/log_codes.yaml
Coverage Checker is a tool that ensures your application logs cover all the log codes defined in the YAML contract. It calculates the percentage of log codes that are covered and logs the uncovered log codes.
-
Create the YAML file (
log_codes.yaml
) at the root of your project. -
Make sure your application logs are in JSON format and include a
Code
field that corresponds to thecode
field in your YAML contract. Pipe these logs into Coverage Checker. -
Run the Coverage Checker using the following command:
cat ./app.log | go run ./cmd/coverage | tee coverage_report.txt
This will output a coverage report, including the log codes that are not covered by your application logs, and write it to coverage_report.txt
.
To test the Coverage Checker, you can run:
go test ./cmd/coverage
Coverage Checker reads the log_codes.yaml
file and the application logs from the standard input. For each log code, it checks whether a corresponding log entry exists.
The getLogCodesFromYaml()
function reads and parses the log_codes.yaml
file to get a list of log codes.
The getLogsFromStdin()
function reads the application logs from the standard input, parses each log entry to extract the Code
, and returns a list of these codes.
In the main()
function, the coverage is checked by comparing the list of log codes from the YAML file and the list of log codes from the application logs. The function logs any log code not covered by the application logs and calculates the coverage percentage.