Skip to content

Commit 069abd9

Browse files
committed
feat(logging): add configurable logging support to opencode
1 parent d864fb8 commit 069abd9

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ require('opencode').setup({
297297
enabled = false,
298298
},
299299
},
300+
logging = {
301+
enabled = false,
302+
level = 'warn', -- debug, info, warn, error
303+
outfile = nil,
304+
},
300305
debug = {
301306
enabled = false, -- Enable debug messages in the output window
302307
capture_streamed_events = false,

lua/opencode/config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ M.defaults = {
216216
enabled = false,
217217
},
218218
},
219+
logging = {
220+
enabled = false,
221+
level = 'info', -- debug, info, warn, error
222+
outfile = nil,
223+
},
219224
debug = {
220225
enabled = false,
221226
capture_streamed_events = false,

lua/opencode/log.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local M = {}
2+
3+
local config = require('opencode.config')
4+
local log_path = config.logging and config.logging.outfile or vim.fn.stdpath('log') .. '/opencode.log'
5+
local level = config.logging and config.logging.level or 'warn'
6+
7+
local logger = require('plenary.log').new({
8+
plugin = 'opencode',
9+
level = level:lower(),
10+
use_console = false,
11+
outfile = log_path,
12+
})
13+
14+
local function get_logger()
15+
return logger
16+
end
17+
18+
function M.debug(msg, ...)
19+
if not config.logging.enabled then
20+
return
21+
end
22+
get_logger().debug(string.format(msg, ...))
23+
end
24+
25+
function M.info(msg, ...)
26+
if not config.logging.enabled then
27+
return
28+
end
29+
get_logger().info(string.format(msg, ...))
30+
end
31+
32+
function M.warn(msg, ...)
33+
if not config.logging.enabled then
34+
return
35+
end
36+
get_logger().warn(string.format(msg, ...))
37+
end
38+
39+
function M.error(msg, ...)
40+
if not config.logging.enabled then
41+
return
42+
end
43+
get_logger().error(string.format(msg, ...))
44+
end
45+
46+
--- @return string
47+
function M.get_path()
48+
if not config.logging.enabled then
49+
return
50+
end
51+
return log_path
52+
end
53+
54+
return M

lua/opencode/types.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@
189189
---@field default_agent? string -- Use current mode if nil
190190
---@field instructions? string[] -- Custom instructions for quick chat
191191

192+
---@class OpencodeLoggingConfig
193+
---@field enabled boolean
194+
---@field level 'debug' | 'info' | 'warn' | 'error'
195+
---@field outfile string|nil
196+
192197
---@class OpencodeConfig
193198
---@field preferred_picker 'telescope' | 'fzf' | 'mini.pick' | 'snacks' | 'select' | nil
194199
---@field preferred_completion 'blink' | 'nvim-cmp' | 'vim_complete' | nil -- Preferred completion strategy for mentons and commands
@@ -200,6 +205,7 @@
200205
---@field keymap OpencodeKeymap
201206
---@field ui OpencodeUIConfig
202207
---@field context OpencodeContextConfig
208+
---@field logging OpencodeLoggingConfig
203209
---@field debug OpencodeDebugConfig
204210
---@field prompt_guard? fun(mentioned_files: string[]): boolean
205211
---@field hooks OpencodeHooks

0 commit comments

Comments
 (0)