|
1 | 1 | package ion |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "time" |
| 4 | + "github.com/JupiterMetaLabs/ion/internal/config" |
6 | 5 | ) |
7 | 6 |
|
8 | 7 | // Config holds the complete logger configuration. |
9 | | -type Config struct { |
10 | | - // Level sets the minimum log level: debug, info, warn, error. |
11 | | - // Default: "info" |
12 | | - Level string `yaml:"level" json:"level" env:"LOG_LEVEL"` |
| 8 | +// It is an alias to internal/config.Config to allow sharing with internal packages. |
| 9 | +type Config = config.Config |
13 | 10 |
|
14 | | - // Development enables development mode with: |
15 | | - // - Pretty console output by default |
16 | | - // - Caller information in logs |
17 | | - // - Stack traces on error/fatal |
18 | | - Development bool `yaml:"development" json:"development" env:"LOG_DEVELOPMENT"` |
| 11 | +// ConsoleConfig configures console output. |
| 12 | +type ConsoleConfig = config.ConsoleConfig |
19 | 13 |
|
20 | | - // ServiceName identifies this service in logs and OTEL. |
21 | | - // Default: "unknown" |
22 | | - ServiceName string `yaml:"service_name" json:"service_name" env:"SERVICE_NAME"` |
| 14 | +// FileConfig configures file output. |
| 15 | +type FileConfig = config.FileConfig |
23 | 16 |
|
24 | | - // Version is the application version, included in logs. |
25 | | - Version string `yaml:"version" json:"version" env:"SERVICE_VERSION"` |
| 17 | +// OTELConfig configures OTEL log export. |
| 18 | +type OTELConfig = config.OTELConfig |
26 | 19 |
|
27 | | - // Console output configuration. |
28 | | - Console ConsoleConfig `yaml:"console" json:"console"` |
29 | | - |
30 | | - // File output configuration (with rotation). |
31 | | - File FileConfig `yaml:"file" json:"file"` |
32 | | - |
33 | | - // OTEL (OpenTelemetry) exporter configuration. |
34 | | - OTEL OTELConfig `yaml:"otel" json:"otel"` |
35 | | -} |
36 | | - |
37 | | -// ConsoleConfig configures console (stdout/stderr) output. |
38 | | -type ConsoleConfig struct { |
39 | | - // Enabled controls whether console output is active. |
40 | | - // Default: true |
41 | | - Enabled bool `yaml:"enabled" json:"enabled"` |
42 | | - |
43 | | - // Format: "json" for structured JSON, "pretty" for human-readable. |
44 | | - // Default: "json" (production), "pretty" (development) |
45 | | - Format string `yaml:"format" json:"format"` |
46 | | - |
47 | | - // Color enables ANSI colors in pretty format. |
48 | | - // Default: true |
49 | | - Color bool `yaml:"color" json:"color"` |
50 | | - |
51 | | - // ErrorsToStderr sends warn/error/fatal to stderr, others to stdout. |
52 | | - // Default: true |
53 | | - ErrorsToStderr bool `yaml:"errors_to_stderr" json:"errors_to_stderr"` |
54 | | -} |
55 | | - |
56 | | -// FileConfig configures file output with rotation. |
57 | | -type FileConfig struct { |
58 | | - // Enabled controls whether file output is active. |
59 | | - // Default: false |
60 | | - Enabled bool `yaml:"enabled" json:"enabled"` |
61 | | - |
62 | | - // Path is the log file path. |
63 | | - // Example: "/var/log/app/app.log" |
64 | | - Path string `yaml:"path" json:"path"` |
65 | | - |
66 | | - // MaxSizeMB is the maximum size in MB before rotation. |
67 | | - // Default: 100 |
68 | | - MaxSizeMB int `yaml:"max_size_mb" json:"max_size_mb"` |
69 | | - |
70 | | - // MaxAgeDays is the maximum age in days to retain old logs. |
71 | | - // Default: 7 |
72 | | - MaxAgeDays int `yaml:"max_age_days" json:"max_age_days"` |
73 | | - |
74 | | - // MaxBackups is the maximum number of old log files to keep. |
75 | | - // Default: 5 |
76 | | - MaxBackups int `yaml:"max_backups" json:"max_backups"` |
77 | | - |
78 | | - // Compress enables gzip compression of rotated log files. |
79 | | - // Default: true |
80 | | - Compress bool `yaml:"compress" json:"compress"` |
81 | | -} |
82 | | - |
83 | | -// OTELConfig configures OpenTelemetry log export. |
84 | | -type OTELConfig struct { |
85 | | - // Enabled controls whether OTEL export is active. |
86 | | - // Default: false |
87 | | - Enabled bool `yaml:"enabled" json:"enabled"` |
88 | | - |
89 | | - // Protocol: "grpc" or "http". gRPC is recommended for performance. |
90 | | - // Default: "grpc" |
91 | | - Protocol string `yaml:"protocol" json:"protocol"` |
92 | | - |
93 | | - // Endpoint is the OTEL collector endpoint. |
94 | | - // Examples: "localhost:4317" (gRPC), "localhost:4318/v1/logs" (HTTP) |
95 | | - Endpoint string `yaml:"endpoint" json:"endpoint"` |
96 | | - |
97 | | - // Insecure disables TLS for the connection. |
98 | | - // Default: false |
99 | | - Insecure bool `yaml:"insecure" json:"insecure"` |
100 | | - |
101 | | - // Username for Basic Authentication (optional). |
102 | | - Username string `yaml:"username" json:"username" env:"OTEL_USERNAME"` |
103 | | - |
104 | | - // Password for Basic Authentication (optional). |
105 | | - Password string `yaml:"password" json:"password" env:"OTEL_PASSWORD"` |
106 | | - |
107 | | - // Headers are additional headers to send (e.g., auth tokens). |
108 | | - Headers map[string]string `yaml:"headers" json:"headers"` |
109 | | - |
110 | | - // Timeout is the export timeout. |
111 | | - // Default: 10s |
112 | | - Timeout time.Duration `yaml:"timeout" json:"timeout"` |
113 | | - |
114 | | - // BatchSize is the number of logs per export batch. |
115 | | - // Default: 512 |
116 | | - BatchSize int `yaml:"batch_size" json:"batch_size"` |
117 | | - |
118 | | - // ExportInterval is how often to export batched logs. |
119 | | - // Default: 5s |
120 | | - ExportInterval time.Duration `yaml:"export_interval" json:"export_interval"` |
121 | | - |
122 | | - // Attributes are additional resource attributes for OTEL. |
123 | | - // Example: {"environment": "production", "chain": "solana"} |
124 | | - Attributes map[string]string `yaml:"attributes" json:"attributes"` |
125 | | -} |
| 20 | +// TracingConfig configures distributed tracing. |
| 21 | +type TracingConfig = config.TracingConfig |
126 | 22 |
|
127 | 23 | // Default returns a Config with sensible production defaults. |
128 | 24 | func Default() Config { |
129 | | - return Config{ |
130 | | - Level: "info", |
131 | | - Development: false, |
132 | | - ServiceName: "unknown", |
133 | | - Console: ConsoleConfig{ |
134 | | - Enabled: true, |
135 | | - Format: "json", |
136 | | - Color: true, |
137 | | - ErrorsToStderr: true, |
138 | | - }, |
139 | | - File: FileConfig{ |
140 | | - Enabled: false, |
141 | | - MaxSizeMB: 100, |
142 | | - MaxAgeDays: 7, |
143 | | - MaxBackups: 5, |
144 | | - Compress: true, |
145 | | - }, |
146 | | - OTEL: OTELConfig{ |
147 | | - Enabled: false, |
148 | | - Protocol: "grpc", |
149 | | - Insecure: false, |
150 | | - Timeout: 10 * time.Second, |
151 | | - BatchSize: 512, |
152 | | - ExportInterval: 5 * time.Second, |
153 | | - }, |
154 | | - } |
| 25 | + return config.Default() |
155 | 26 | } |
156 | 27 |
|
157 | 28 | // Development returns a Config optimized for development. |
158 | 29 | func Development() Config { |
159 | | - cfg := Default() |
160 | | - cfg.Level = "debug" |
161 | | - cfg.Development = true |
162 | | - cfg.Console.Format = "pretty" |
163 | | - return cfg |
164 | | -} |
165 | | - |
166 | | -// WithLevel returns a copy of the config with the specified level. |
167 | | -func (c Config) WithLevel(level string) Config { |
168 | | - c.Level = level |
169 | | - return c |
170 | | -} |
171 | | - |
172 | | -// WithService returns a copy of the config with the specified service name. |
173 | | -func (c Config) WithService(name string) Config { |
174 | | - c.ServiceName = name |
175 | | - return c |
176 | | -} |
177 | | - |
178 | | -// WithOTEL returns a copy of the config with OTEL enabled. |
179 | | -func (c Config) WithOTEL(endpoint string) Config { |
180 | | - c.OTEL.Enabled = true |
181 | | - c.OTEL.Endpoint = endpoint |
182 | | - return c |
183 | | -} |
184 | | - |
185 | | -// WithFile returns a copy of the config with file logging enabled. |
186 | | -func (c Config) WithFile(path string) Config { |
187 | | - c.File.Enabled = true |
188 | | - c.File.Path = path |
189 | | - return c |
190 | | -} |
191 | | - |
192 | | -// InitFromEnv initializes a logger using environment variables. |
193 | | -// Supported variables: |
194 | | -// - LOG_LEVEL: debug, info, warn, error (default: info) |
195 | | -// - SERVICE_NAME: name of the service (default: unknown) |
196 | | -// - LOG_DEVELOPMENT: "true" for pretty console output |
197 | | -// - OTEL_ENDPOINT: if set, enables OpenTelemetry export |
198 | | -func InitFromEnv() Logger { |
199 | | - cfg := Default() |
200 | | - |
201 | | - if val := os.Getenv("LOG_LEVEL"); val != "" { |
202 | | - cfg.Level = val |
203 | | - } |
204 | | - if val := os.Getenv("SERVICE_NAME"); val != "" { |
205 | | - cfg.ServiceName = val |
206 | | - } |
207 | | - if os.Getenv("LOG_DEVELOPMENT") == "true" { |
208 | | - cfg.Development = true |
209 | | - cfg.Console.Format = "pretty" |
210 | | - } |
211 | | - if val := os.Getenv("OTEL_ENDPOINT"); val != "" { |
212 | | - cfg.OTEL.Enabled = true |
213 | | - cfg.OTEL.Endpoint = val |
214 | | - } |
215 | | - |
216 | | - if cfg.OTEL.Enabled && cfg.OTEL.Endpoint != "" { |
217 | | - l, err := NewWithOTEL(cfg) |
218 | | - if err == nil { |
219 | | - return l |
220 | | - } |
221 | | - } |
222 | | - |
223 | | - return New(cfg) |
| 30 | + return config.Development() |
224 | 31 | } |
0 commit comments