Skip to content

Commit

Permalink
elixir based logger levels
Browse files Browse the repository at this point in the history
  • Loading branch information
joshamb committed Mar 12, 2024
1 parent f3796ab commit fc61192
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/phoenix_live_reload/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Phoenix.LiveReloader.Channel do
alias Phoenix.LiveReloader.WebConsoleLogger

@logs :logs
@logger_levels [:emergency, :alert, :critical, :error, :warning, :notice, :info, :debug]

def join("phoenix:live_reload", _msg, socket) do
{:ok, _} = Application.ensure_all_started(:phoenix_live_reload)
Expand Down Expand Up @@ -128,5 +129,6 @@ defmodule Phoenix.LiveReloader.Channel do
else
%{}
end
|> Map.put(:logger_levels, @logger_levels)
end
end
13 changes: 11 additions & 2 deletions priv/static/phoenix_live_reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class LiveReloader {
this.logsEnabled = false
this.enabledOnce = false
this.editorURL = null
this.minLogLevel = "debug"
this.loggerLevels = []
}
enable(){
this.socket.onOpen(() => {
Expand All @@ -70,9 +72,10 @@ class LiveReloader {
let reloadStrategy = reloadStrategies[msg.asset_type] || reloadStrategies.page
setTimeout(() => reloadStrategy(this.channel), interval)
})
this.channel.on("log", ({msg, level}) => this.logsEnabled && this.log(level, msg))
this.channel.join().receive("ok", ({editor_url}) => {
this.channel.on("log", ({msg, level}) => this.logsEnabled && this.isMinLogLevel(level) && this.log(level, msg))
this.channel.join().receive("ok", ({editor_url, logger_levels}) => {
this.editorURL = editor_url
this.loggerLevels = logger_levels
})
this.socket.connect()
}
Expand All @@ -85,6 +88,12 @@ class LiveReloader {
enableServerLogs(){ this.logsEnabled = true }
disableServerLogs(){ this.logsEnabled = false }

setMinLogLevel(level){ this.minLogLevel = level }

isMinLogLevel(level){
return this.loggerLevels.indexOf(level) <= this.loggerLevels.indexOf(this.minLogLevel)
}

openEditorAtCaller(targetNode){
if(!this.editorURL){
return console.error("phoenix_live_reload cannot openEditorAtCaller without configured PLUG_EDITOR")
Expand Down

0 comments on commit fc61192

Please sign in to comment.