Logging & Error Handling
การบั นทึก
Casbin can log requests, model load, and policy changes. Logging is off by default. Turn it on with Enforcer.EnableLog() or via the logger parameter to NewEnforcer(). Example output:
2017/07/15 19:43:56 [Request: alice, data1, read ---> true]
Go: You can plug in a custom logger; Casbin logs models, requests, roles, and policies through it.
Python: PyCasbin uses the standard logging module. Configure your app’s logging as usual; PyCasbin uses logging.getLogger(). Optional logging_config is documented in pycasbin/util/log.py. See Python logging and Django logging for setup.
Configure Separate Loggers for Different Enforcers
Each enforcer can have its own logger, and loggers can be changed at runtime.
You can specify a logger using the last parameter of NewEnforcer(). When using this method, the logger's enabled field takes precedence over the enforcer's enabled parameter.
// Set a default logger as enforcer e1's logger.
// This operation can also be seen as changing the logger of e1 at runtime.
e1.SetLogger(&Log.DefaultLogger{})
// Set another logger as enforcer e2's logger.
e2.SetLogger(&YouOwnLogger)
// Set your logger when initializing enforcer e3.
e3, _ := casbin.NewEnforcer("examples/rbac_model.conf", a, logger)
Supported Loggers
Casbin provides several logger implementations:
| Logger | Author | Description |
|---|---|---|
| Default logger (built-in) | Casbin | The default logger using Go's standard log package. |
| Zap logger | Casbin | Uses zap for JSON-encoded logs with additional customization options via zap-logger. |
| Logger | Author | คำอธิบาย |
|---|---|---|
| psr3-bridge logger | Casbin | Provides a PSR-3 compliant bridge. |