Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add config to control log level #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/gp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ local config = {
-- write sensitive data to log file for debugging purposes (like api keys)
log_sensitive = false,

log_level = vim.log.levels.DEBUG,

-- directory for persisting state dynamically changed by user (like model or persona)
state_dir = vim.fn.stdpath("data"):gsub("/$", "") .. "/gp/persisted",

Expand Down
3 changes: 2 additions & 1 deletion lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ M.setup = function(opts)
local state_dir = opts.state_dir or M.config.state_dir
local openai_api_key = opts.openai_api_key or M.config.openai_api_key

M.logger.setup(opts.log_file or M.config.log_file, opts.log_sensitive)
local log_level = opts.log_level == nil and M.config.log_level or opts.log_level
M.logger.setup(opts.log_file or M.config.log_file, opts.log_sensitive, log_level)

M.vault.setup({ state_dir = state_dir, curl_params = curl_params })

Expand Down
13 changes: 8 additions & 5 deletions lua/gp/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local uv = vim.uv or vim.loop
local M = {}

local file = "/dev/null"
local log_level = vim.log.levels.DEBUG
local uuid = ""
local store_sensitive = false

Expand All @@ -25,9 +26,11 @@ end

---@param path string # path to log file
---@param sensitive boolean | nil # whether to store sensitive data in logs
M.setup = function(path, sensitive)
---@param level number
M.setup = function(path, sensitive, level)
store_sensitive = sensitive or false
uuid = string.format("%x", math.random(0, 0xFFFF)) .. string.format("%x", os.time() % 0xFFFF)
log_level = level
M.debug("New neovim instance [" .. uuid .. "] started, setting log file to " .. path)
local dir = vim.fn.fnamemodify(path, ":h")
if vim.fn.isdirectory(dir) == 0 then
Expand Down Expand Up @@ -70,6 +73,10 @@ end
---@param slevel string # log level as string
---@param sensitive boolean | nil # sensitive log
local log = function(msg, level, slevel, sensitive)
if level <= log_level then
return
end

local raw = msg
if sensitive then
if not store_sensitive then
Expand All @@ -92,10 +99,6 @@ local log = function(msg, level, slevel, sensitive)
log_file:close()
end

if level <= vim.log.levels.DEBUG then
return
end

vim.schedule(function()
vim.notify("Gp.nvim: " .. msg, level, { title = "Gp.nvim" })
end)
Expand Down