Welcome to Gaudit, an elegant and powerful auditing package for Go applications! With Gaudit, you can effortlessly track changes, log activities, and maintain a detailed audit trail of your data.
- Comprehensive Auditing: Automatically log changes for insert and update operations.
- Flexible Hooks: Use pre-defined hooks or create your own to customize auditing behavior.
- Rich Metadata: Capture essential details such as user actions, timestamps, and IP addresses.
- Easy Integration: Seamlessly integrate with your existing Go applications.
- Extensible: Extend the package with your own custom functionalities.
To get started with Gaudit, install it using Go modules:
go get github.com/its-own/gaudit
Here's a quick example of how to use Gaudit in your application:
//main.go
package main
import (
"context"
"fmt"
"github.com/its-own/gaudit"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log/slog"
)
func main() {
// connect to mongo db
ctx := context.Background()
client := connectMongo(ctx, "mongodb://localhost:27017")
// initialize go audit
aMgo := gaudit.Init(&gaudit.Config{
Client: client,
Database: client.Database("test_database"),
Logger: slog.Default(),
})
// create user and pass gaudit mongo instance
_, err := NewUserRepo("user", aMgo).Create(ctx, &User{
ID: primitive.NewObjectID(),
Name: "Razibul Hasan Mithu",
})
if err != nil {
return
}
}
//repo.go
package main
import (
"context"
"github.com/its-own/gaudit/db"
"github.com/its-own/gaudit/in"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type User struct {
in.Inject
ID primitive.ObjectID `bson:"_id" json:"id"`
Name string `bson:"name" json:"name"`
}
// IUserRepo is a User repository
type IUserRepo interface {
Create(ctx context.Context, param *User) (*User, error)
}
// UserRepo implementation of IUserRepo, also holds collection name and mongo db rapper repository
type UserRepo struct {
collection string
connection db.NoSql
}
func NewUserRepo(collection string, connection db.NoSql) IUserRepo {
return &UserRepo{connection: connection, collection: collection}
}
// Create is a simple implementation of user repository
func (u UserRepo) Create(ctx context.Context, param *User) (*User, error) {
err := u.connection.Insert(ctx, u.collection, param)
if err != nil {
return nil, err
}
return param, nil
}
Customize Gaudit to fit your needs. You can configure logging settings, output formats, and more in the config.go
file.
// Example of setting configuration
gaudit.SetConfig(gaudit.Config{
LogLevel: "debug",
// other configurations...
})
coming soon
Gaudit comes with a robust set of tests to ensure stability. Run the tests with:
go test ./...
We welcome contributions! If you'd like to contribute to Gaudit, please fork the repo and create a pull request. For larger changes, please open an issue first to discuss.
- Fork the repository
- Create a new branch (
git checkout -b feature/my-feature
) - Make your changes
- Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/my-feature
) - Open a Pull Request
For any questions, suggestions, or feedback, feel free to reach out via Issues or contact us directly at razibulhasan.mithu@gmail.com.
This project is licensed under the MIT License - see the LICENSE file for details.
Thank you for considering Gaudit for your auditing needs! We hope it enhances your application's capabilities. Happy coding! 🚀