Documentation
¶
Overview ¶
Package database provides a custom logger implementation for GORM, a popular ORM library for Go. This logger allows customization of logger messages and formatting, including support for colorful output.
Version: 0.0.1 License: Apache License 2.0
Author: dexterdmonkey
This package includes a custom logger that integrates with GORM's logging interface to provide customizable logging behavior for database operations. It supports different log levels and formatting options, including colorful output for enhanced readability.
Example usage:
// Initialize a new logger with custom configurations logger := NewLogger(os.Stdout, logger.Config{ Colorful: true, LogLevel: logger.Info, }) // Integrate the logger with a GORM database instance db := &PostgreSQL{ DB: gormDB, DatabaseLogger: logger, } // Use the database instance with integrated logger for database operations
Index ¶
- func NewLogger(writer logger.Writer, config logger.Config) *dbLogger
- type Config
- type PostgreSQL
- func (db *PostgreSQL) DebugMode()
- func (l PostgreSQL) Error(ctx context.Context, msg string, data ...interface{})
- func (l PostgreSQL) Info(ctx context.Context, msg string, data ...interface{})
- func (l PostgreSQL) LogMode(level logger.LogLevel) logger.Interface
- func (l PostgreSQL) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{})
- func (db *PostgreSQL) SetLogger(writer logger.Writer)
- func (db *PostgreSQL) SetMaxConnectionPool(n int) error
- func (db *PostgreSQL) SetMinConnectionPool(n int) error
- func (l PostgreSQL) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l PostgreSQL) Warn(ctx context.Context, msg string, data ...interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { Host string // Database host address. Port int // Database port number. User string // Database user name. Pass string // Database password. Name string // Database name. MaxConnectionPool int // Maximum size of the connection pool. Set to <= 0 for unlimited connections. Default is 0. MinConnectionPool int // Minimum size of the connection pool. Set to <= 0 for no connection pooling. Default is 0. Timezone string // Timezone of the database server. Default is "Asia/Jakarta". }
Config holds configuration parameters for connecting to a database.
type PostgreSQL ¶
PostgreSQL implements the Interface for a PostgreSQL database using GORM.
func CreatePostgreSQL ¶
func CreatePostgreSQL(cfg *Config) (*PostgreSQL, error)
CreatePostgreSQL initializes a new PostgreSQL database connection using the provided configuration.
func (*PostgreSQL) DebugMode ¶
func (db *PostgreSQL) DebugMode()
DebugMode sets the logger to debug mode for detailed logging of SQL queries and transactions. When enabled, the logger will output detailed information for each SQL query or transaction executed. This includes logging SQL statements, execution time, and number of affected rows.
Example:
db := database.New(...) db.DebugMode()
Notes:
- Debug mode should be used primarily for development and debugging purposes.
- Enabling debug mode may impact performance due to increased logging overhead.
func (PostgreSQL) LogMode ¶
LogMode sets the logger's log level and returns a new logger instance with the updated settings.
func (PostgreSQL) ParamsFilter ¶
func (l PostgreSQL) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{})
ParamsFilter filters sensitive parameters from SQL statements if ParameterizedQueries is enabled.
func (*PostgreSQL) SetLogger ¶
func (db *PostgreSQL) SetLogger(writer logger.Writer)
SetLogger sets a custom logger for the database.
func (*PostgreSQL) SetMaxConnectionPool ¶
func (db *PostgreSQL) SetMaxConnectionPool(n int) error
SetMaxConnectionPool sets the maximum number of open connections to the database. It configures the PostgreSQL database connection to allow up to 'n' concurrent open connections.
Parameters:
n (int): Maximum number of open connections. Set to 0 or a negative value for unlimited connections.
Returns:
error: An error if setting the maximum open connections fails.
Example:
db := database.New(...) err := db.SetMaxConnectionPool(20) if err != nil { fmt.Println("Error setting max connection pool:", err) }
func (*PostgreSQL) SetMinConnectionPool ¶
func (db *PostgreSQL) SetMinConnectionPool(n int) error
SetMinConnectionPool sets the minimum number of idle connections to the database. It configures the PostgreSQL database connection to maintain at least 'n' idle connections when available.
Parameters:
n (int): Minimum number of idle connections. Set to 0 or a negative value to disable idle connections.
Returns:
error: An error if setting the minimum idle connections fails.
Example:
db := database.New(...) err := db.SetMinConnectionPool(5) if err != nil { fmt.Println("Error setting min connection pool:", err) }