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

refactor(logger): adapt to new changes upstream #3695

Merged
merged 1 commit into from
Jan 19, 2023

Conversation

Tastyep
Copy link
Member

@Tastyep Tastyep commented Jan 8, 2023

Description

Refactor structlog usage for future 0.2 release

  • Add a flag for disabling vim.notify logging as notification (to discuss)

How Has This Been Tested?

  • Run lvim
  • Log something
  • Check that it is logged into all sinks

Related work

Tastyep/structlog.nvim#6

@LostNeophyte
Copy link
Member

@Tastyep there's a bug here, it doesn't allow setting async to false, false or true returns true

@Tastyep
Copy link
Member Author

Tastyep commented Jan 8, 2023

@Tastyep there's a bug here, it doesn't allow setting async to false, false or true returns true

Thanks for the heads-up :)
Tastyep/structlog.nvim@fbfec9d

Add a flag for disabling vim.notify logging as notification (to discuss)
@LostNeophyte
Copy link
Member

Something like this is needed, otherwise there are errors after updating, we can't be sure that the structlog version is always the one specified in the snapshot

diff --git a/lua/lvim/core/log.lua b/lua/lvim/core/log.lua
index 2602e64..27adc2a 100644
--- a/lua/lvim/core/log.lua
+++ b/lua/lvim/core/log.lua
@@ -13,21 +13,27 @@ local notify_opts = {}
 local log_notify_as_notification = false
 
 function Log:set_level(level)
-  local logger_ok, logger = pcall(function()
-    return require("structlog").get_logger "lvim"
-  end)
-  local log_level = Log.levels[level:upper()]
-  if logger_ok and logger and log_level then
-    for _, pipeline in ipairs(logger.pipelines) do
-      pipeline.level = log_level
-    end
-  end
+  if
+    not pcall(function()
+      local logger_ok, logger = pcall(function()
+        return require("structlog").get_logger "lvim"
+      end)
+      local log_level = Log.levels[level:upper()]
+      if logger_ok and logger and log_level then
+        for _, pipeline in ipairs(logger.pipelines) do
+          pipeline.level = log_level
+        end
+      end
 
-  local packer_ok, _ = xpcall(function()
-    require("packer.log").cfg { log = { level = level } }
-  end, debug.traceback)
-  if not packer_ok then
-    vim.notify_once("Unable to set packer's log level to " .. level)
+      local packer_ok, _ = xpcall(function()
+        require("packer.log").cfg { log = { level = level } }
+      end, debug.traceback)
+      if not packer_ok then
+        vim.notify_once("Unable to set packer's log level to " .. level)
+      end
+    end)
+  then
+    vim.cmd "echo 'structlog version too old, run `:Lazy sync`'"
   end
 end
 
@@ -130,11 +136,17 @@ end
 ---@param msg any
 ---@param event any
 function Log:add_entry(level, msg, event)
-  local logger = self:get_logger()
-  if not logger then
-    return
+  if
+    not pcall(function()
+      local logger = self:get_logger()
+      if not logger then
+        return
+      end
+      logger:log(level, vim.inspect(msg), event)
+    end)
+  then
+    vim.cmd "echo 'structlog version too old, run `:Lazy sync`'"
   end
-  logger:log(level, vim.inspect(msg), event)
 end
 
 ---Retrieves the handle of the logger object

@Tastyep
Copy link
Member Author

Tastyep commented Jan 14, 2023

Indeed, it is not something I thought about, since when testing it I removed the directory manually. Could it help if structlog would expose its version number (structlog.version) ?
Maybe in the future it would make sens to implement some kind of configuration migration mechanism on structlog's side.

On another topic, I added the local log_notify_as_notification variable. The intent is just to show that messages can be discarded by using the SinkAdapter. I understand that since we redirect vim.notify calls to the logger, it would generate a notification for each call, which are way too many. Maybe something that could be considered is to disable notifications when the entry point is vim.notify, but enable them when the logger is used directly (by lvim). Tell me what you think about it.

@LostNeophyte
Copy link
Member

Could it help if structlog would expose its version number (structlog.version)

With this we could add a mechanism update structlog on startup if the version is too old, and then remove it on next release, but I'm not sure it it's worth it, updates between releases remove the plugins directory anyway so it's just for master users

On another topic, I added the local log_notify_as_notification variable. The intent is just to show that messages can be discarded by using the SinkAdapter. I understand that since we redirect vim.notify calls to the logger, it would generate a notification for each call, which are way too many. Maybe something that could be considered is to disable notifications when the entry point is vim.notify, but enable them when the logger is used directly (by lvim). Tell me what you think about it.

configure_notifications isn't called anywhere currently, I wasn't on the team when it was so I'm not the best person to answer this, but personally I think vim.notify messages should be shown to the user

Copy link
Collaborator

@kylo252 kylo252 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kylo252
Copy link
Collaborator

kylo252 commented Jan 19, 2023

On another topic, I added the local log_notify_as_notification variable. The intent is just to show that messages can be discarded by using the SinkAdapter. I understand that since we redirect vim.notify calls to the logger, it would generate a notification for each call, which are way too many. Maybe something that could be considered is to disable notifications when the entry point is vim.notify, but enable them when the logger is used directly (by lvim). Tell me what you think about it.

Log:configure_notifications(nvim_notify) has been more or less unsupported since we dropped nvim-notify. Let's merge this for now since it's blocking the CI, and we can consider further changes to it later.

@kylo252 kylo252 changed the title refactor(logging): refactor structlog usage to prepare 0.2 release refactor(logger): adapt to new changes upstream Jan 19, 2023
@kylo252 kylo252 merged commit e7d7aa9 into LunarVim:master Jan 19, 2023
@yannk
Copy link

yannk commented Jan 19, 2023

I had to go into ~/.local/share/lunarvim/site/pack/lazy/opt/structlog.nvim and force checkout 45b26a2 after this was merged to have lvim not complain about this.

pack/lazy/opt/structlog.nvim/lua/structlog/logger.lua:49: bad argument #1 to 'ipairs' (table expected, got nil)

@kylo252 @LostNeophyte

@LostNeophyte
Copy link
Member

LostNeophyte commented Jan 19, 2023

I had to go into ~/.local/share/lunarvim/site/pack/lazy/opt/structlog.nvim and force checkout 45b26a2 after this was merged to have lvim not complain about this.
pack/lazy/opt/structlog.nvim/lua/structlog/logger.lua:49: bad argument #1 to 'ipairs' (table expected, got nil)

that's what I was talking about here #3695 (comment),
pr to fix the issue #3755

tomazursic pushed a commit to tomazursic/LunarVim that referenced this pull request Jan 24, 2023
* upstream/master:
  refactor!: remove `%` and `$` autopairs rules (LunarVim#3759)
  chore: bump plugins version (LunarVim#3761)
  feat: add crystal filetype (LunarVim#3762)
  fix(lsp): info diagnostic icon not showing (LunarVim#3756)
  fix(logger): fix errors with older structlog versions (LunarVim#3755)
  chore: bump plugins version (LunarVim#3679)
  refactor(logger): adapt to new changes upstream (LunarVim#3695)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants