Skip to content

Commit ee7bdbd

Browse files
authored
Introduce cider-log-show function (#3602)
* Introduce `cider-log-show` function * Prefer logview-mode unconditionally, and warn when it's absent This way, users will know logview-mode exists. There's still fallback, and optionality.
1 parent 07150f1 commit ee7bdbd

File tree

4 files changed

+153
-33
lines changed

4 files changed

+153
-33
lines changed

cider-log.el

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
(require 'transient)
3838

3939
(defcustom cider-log-framework-name nil
40-
"The name of the current log framework."
40+
"The name of the default log framework.
41+
42+
You may want to set this in .dir-locals.el,
43+
for a more streamlined setup.
44+
45+
Example value: \"Logback\"."
4146
:group 'cider
4247
:package-version '(cider . "1.8.0")
4348
:safe #'stringp
@@ -85,13 +90,20 @@
8590
:safe #'integerp
8691
:type 'integer)
8792

88-
(defcustom cider-log-use-logview (fboundp 'logview-mode)
89-
"Whether to use `logview-mode' or not."
93+
(defcustom cider-log-use-logview t
94+
"Whether to use `logview-mode'.
95+
96+
It will not be used if the package hasn't been installed."
9097
:group 'cider
9198
:package-version '(cider . "1.8.0")
9299
:safe #'booleanp
93100
:type 'boolean)
94101

102+
(defun cider-log-use-logview ()
103+
"Whether to use `logview-mode'."
104+
(and (fboundp 'logview-mode)
105+
cider-log-use-logview))
106+
95107
(defvar logview-mode-map)
96108
(declare-function logview--guess-submode "logview" () t)
97109
(declare-function logview-initialized-p "logview" () t)
@@ -556,6 +568,8 @@ The KEYS are used to lookup the values and are joined by SEPARATOR."
556568
(when-let (appender (cider-log-appender-reload framework appender))
557569
(cider-log-appender-consumer appender consumer)))
558570

571+
(declare-function cider-log-mode "cider-log")
572+
559573
(defun cider-log--consumer-add (framework appender consumer buffer)
560574
"Add the CONSUMER to the APPENDER of FRAMEWORK and write events to BUFFER."
561575
(cider-request:log-add-consumer
@@ -567,7 +581,14 @@ The KEYS are used to lookup the values and are joined by SEPARATOR."
567581
(setq-local cider-log-framework framework)
568582
(setq-local cider-log-appender appender)
569583
(setq cider-log-consumer cider/log-add-consumer)
570-
(switch-to-buffer buffer)))
584+
(switch-to-buffer buffer)
585+
(when (and cider-log-use-logview
586+
(not (fboundp 'logview-mode)))
587+
(message "[CIDER Log Mode] Please install the Logview package for best results.")
588+
(cider--display-interactive-eval-result "Please install the Logview package for best results."
589+
'error
590+
(point)
591+
'cider-error-overlay-face))))
571592
((member "cider/log-event" status)
572593
(let* ((consumer (nrepl-dict "id" cider/log-consumer))
573594
(buffers (cider-log-consumer-buffers consumer)))
@@ -580,7 +601,8 @@ The KEYS are used to lookup the values and are joined by SEPARATOR."
580601
(seq-doseq (buffer buffers)
581602
(with-current-buffer buffer
582603
(cider-log--insert-events buffer (list cider/log-event))
583-
(when (and cider-log-use-logview (not (logview-initialized-p)))
604+
(when (and (cider-log-use-logview)
605+
(not (logview-initialized-p)))
584606
(let ((framework cider-log-framework)
585607
(appender cider-log-appender)
586608
(consumer cider-log-consumer))
@@ -812,7 +834,9 @@ The KEYS are used to lookup the values and are joined by SEPARATOR."
812834

813835
(defvar cider-log-mode-map
814836
(let ((map (make-sparse-keymap))
815-
(parent (if cider-log-use-logview logview-mode-map special-mode-map)))
837+
(parent (if (cider-log-use-logview)
838+
logview-mode-map
839+
special-mode-map)))
816840
(set-keymap-parent map parent)
817841
(define-key map (kbd "C-c M-l a") #'cider-log-appender)
818842
(define-key map (kbd "C-c M-l c") #'cider-log-consumer)
@@ -849,7 +873,7 @@ the CIDER Inspector and the CIDER stacktrace mode.
849873
850874
\\{cider-log-mode-map}")
851875

852-
(if cider-log-use-logview
876+
(if (cider-log-use-logview)
853877
(define-derived-mode cider-log-mode logview-mode "Cider Log" cider-log--mode-doc
854878
(cider-log--setup-mode))
855879
(define-derived-mode cider-log-mode special-mode "Cider Log" cider-log--mode-doc
@@ -1154,12 +1178,17 @@ the CIDER Inspector and the CIDER stacktrace mode.
11541178
(let ((inhibit-read-only t))
11551179
(erase-buffer)))))
11561180

1181+
(defun cider-log--switch-to-buffer (buffer)
1182+
"Switch to the Cider log event BUFFER."
1183+
(when (get-buffer-create buffer)
1184+
(switch-to-buffer-other-window buffer)
1185+
(get-buffer buffer)))
1186+
11571187
(transient-define-suffix cider-log-switch-to-buffer (buffer)
11581188
"Switch to the Cider log event BUFFER."
11591189
:description "Switch to the log event buffer"
11601190
(interactive (list cider-log-buffer))
1161-
(when (get-buffer-create buffer)
1162-
(switch-to-buffer-other-window buffer)))
1191+
(cider-log--switch-to-buffer buffer))
11631192

11641193
(transient-define-suffix cider-log--do-search-events (framework appender filters)
11651194
"Search the log events of FRAMEWORK and APPENDER which match FILTERS."
@@ -1385,6 +1414,27 @@ the CIDER Inspector and the CIDER stacktrace mode.
13851414
(cider-log--ensure-initialized framework appender)
13861415
(transient-setup 'cider-log-event))
13871416

1417+
;;;###autoload
1418+
(defun cider-log-show ()
1419+
"Ensures the *cider-log* buffer is visible,
1420+
setting up a framework, appender and consumer if necessary.
1421+
1422+
Honors the `cider-log-framework-name' customization variable.
1423+
1424+
This function is offered as an alternative to workflows
1425+
based on `transient-mode'."
1426+
(interactive)
1427+
(cider-current-repl nil 'ensure)
1428+
(let ((framework (cider-log--framework))
1429+
(appender (cider-log--appender))
1430+
(new-default-directory (buffer-local-value 'default-directory (current-buffer))))
1431+
(with-current-buffer (cider-log--switch-to-buffer cider-log-buffer)
1432+
(setq-local default-directory new-default-directory) ;; for Sesman
1433+
(cider-log--ensure-initialized framework appender)
1434+
(unless cider-log-consumer
1435+
(apply #'cider-log--ensure-initialized (cider-log--consumer-interactive-list))
1436+
(call-interactively 'cider-log--do-add-consumer)))))
1437+
13881438
;; Main Transient Menu
13891439

13901440
;;;###autoload (autoload 'cider-log "cider-log" "Show the Cider log menu." t)

cider-mode.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ higher precedence."
537537
(define-key map (kbd "C-c M-l a") #'cider-log-appender)
538538
(define-key map (kbd "C-c M-l c") #'cider-log-consumer)
539539
(define-key map (kbd "C-c M-l e") #'cider-log-event)
540+
(define-key map (kbd "C-c M-l s") #'cider-log-show)
540541
(define-key map (kbd "C-c M-l f") #'cider-log-framework)
541542
(define-key map (kbd "C-c M-l i") #'cider-log-info)
542543
(define-key map (kbd "C-c M-l l") #'cider-log)
21.5 KB
Loading

doc/modules/ROOT/pages/debugging/logging.adoc

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
CIDER Log Mode allows you to capture, debug, inspect and view log
55
events emitted by Java logging frameworks. The captured log events can
6-
be searched, streamed to the client, pretty printed and are integrated
6+
be searched, streamed to the client, pretty-printed, and are integrated
77
with the CIDER link:inspector.html[Inspector] and
88
link:../usage/dealing_with_errors.html[Stacktrace Mode]. Here is a
99
screenshot of CIDER Log Mode in action.
@@ -20,53 +20,117 @@ logging related actions.
2020

2121
== Features
2222

23-
- Browse Javadocs and website of log framework.
23+
- Browse Javadocs and website of the given log framework.
2424
- Search log events and show them in buffers.
25-
- link:../usage/pretty_printing.html[Pretty Print] log events.
26-
- Show log events in the CIDER link:inspector.html[Inspector]
27-
- Show log event exceptions in the CIDER link:../usage/dealing_with_errors.html[Stacktrace Mode]
28-
- Integration with https://github.com/doublep/logview[logview]
25+
- link:../usage/pretty_printing.html[Pretty-print] log events.
26+
- Show log events in the CIDER link:inspector.html[Inspector].
27+
- Show log event exceptions in the CIDER link:../usage/dealing_with_errors.html[Stacktrace Mode].
28+
- Integration with https://github.com/doublep/logview[logview].
2929

3030
== Dependencies
3131

3232
https://github.com/doublep/logview[Logview] is an optional dependency
3333
of CIDER Log Mode. We recommend using it, since it is responsible for
3434
coloring the log events and it provides other useful features, such as
3535
syntax highlighting, filtering and more. It is used automatically when
36-
available and its use can be customized via `cider-log-use-logview`.
36+
available, and its use can be customized via `cider-log-use-logview`.
3737

38-
== Usage
38+
== `transient-mode`
3939

40-
To use CIDER Log Mode, type kbd:[C-c M-l l] or kbd:[M-x cider-log] in
41-
any buffer that has a CIDER https://github.com/vspinu/sesman[Sesman]
42-
session attached to it. The first time you run the command, it will
40+
It's worth pointing out that most features and workflows of Cider Log Mode are based on https://github.com/magit/transient[transient-mode].
41+
42+
A transient menu is the following widget:
43+
44+
image::cider-log-transient-mode.png[CIDER Log transient menu]
45+
46+
Its usage is mostly self-describing, since each command has its keybinding attached to the description.
47+
48+
== Getting started
49+
50+
To use CIDER Log Mode, there two main ways to get started:
51+
52+
* `M-x cider-log-event`, which uses transient-mode and will not immediately show the logs (you should use transient-mode to show the `+*cider-log*+` buffer)
53+
* `M-x cider-log-show` is a newer function that intends to be an "all-in-one" command, intended for a streamlined experience, which can be useful to get started, or for casual usage.
54+
** It doesn't use transient-mode - it aims to do everything in one step
55+
** It immediately shows the `+*cider-log*+` buffer
56+
57+
NOTE: any of these commands will only succeed in
58+
a buffer that has a CIDER repl (https://github.com/vspinu/sesman[Sesman]
59+
session) attached to it.
60+
61+
=== Via `cider-log-show`
62+
63+
By using `M-x cider-log-show`, all setup and rendering will be performed for you in a single step that doesn't pop up a `transient-mode` menu:
64+
65+
* A framework will be set up, automatically
66+
** You may be prompted for a framework
67+
** You can prevent the prompt by customizing `cider-log-framework-name` (best done in `dir-locals.el`)
68+
* A log appender and a log consumer will be setup, automatically
69+
* The `+*cider-log*+` buffer will be rendered.
70+
71+
All these steps are idempotent, so it's safe to run `M-x cider-log-show` more than once.
72+
73+
You can refine the setup afterwards (e.g. configuring filtering) by running `M-x cider-log`.
74+
75+
=== Via `cider-log-event`
76+
77+
The intended workflow for this function is as follows:
78+
79+
* Run `M-x cider-log-event`
80+
* A framework will be set up, automatically
81+
** You may be prompted for a framework
82+
** You can prevent the prompt by customizing `cider-log-framework-name` (best done in `dir-locals.el`)
83+
* A log appender and a log consumer will be setup, automatically
84+
* The `+*cider-log*+` buffer will _not_ be rendered
85+
** For it to be rendered, press `s` (`Search log events`) within the transient-mode menu.
86+
87+
NOTE: The `+*cider-log*+` buffer might initially be empty, and you may
88+
see a `No log events found` message. This is because nothing has been
89+
logged between adding the appender and searching for events. So, now
90+
would be a good time to run some code that triggers a log event for
91+
the selected framework.
92+
93+
== Advanced usage
94+
95+
`cider-log` is a function that can serve both to set up logging from scratch,
96+
or for tweaking an existing setup (e.g. add filtering to a consumer).
97+
98+
It's considered a lower-level approach.
99+
100+
=== Setup via `cider-log`
101+
102+
Type kbd:[C-c M-l l] or kbd:[M-x cider-log]. The first time you run the command, it will
43103
prompt you to select a log framework to use, and then attach a log
44104
appender to the root logger of the selected framework. After the log
45105
appender has been attached, the `cider-log` command will show a
46106
https://www.gnu.org/software/emacs/manual/html_mono/transient.html[Transient]
47107
menu, from which you can take further actions, like managing the log
48108
framework, appenders, consumers and events.
49109

50-
To view log events and stream them to your client, type kbd:[es]
110+
You should add a log appender and a log consumer at this point - else no log entries will be possibly shown
111+
(which is why `cider-log` is considered a lower-level workflow).
112+
113+
After that, to view log events and stream them to your client, type kbd:[es]
51114
(Search log events) followed by kbd:[s]. This will open the
52115
`+*cider-log*+` buffer showing any log events captured thus far. It will
53116
also add a log consumer to this buffer, which receives newly-arriving
54117
log events.
55118

56-
NOTE: The `+*cider-log*+` buffer might initially be empty, and you may
57-
see a `No log events found` message. This is because nothing has been
58-
logged between adding the appender and searching for events. So, now
59-
would be a good time to run some code that triggers a log event for
60-
the selected framework.
61-
62119
=== Keybindings
63120

64121
|===
65122
| Command | Keyboard shortcut | Description
123+
| `cider-log-event`
124+
| kbd:[C-c M-l e]
125+
| Show the menu to manage log events via a transient-mode menu. This is one of the two main entrypoints to get started.
126+
127+
| `cider-log-show`
128+
| kbd:[C-c M-l s]
129+
| Immediately shows the logs, without a transient-mode menu. This is one of the two main entrypoints to get started.
66130

67131
| `cider-log`
68132
| kbd:[C-c M-l l]
69-
| Show the CIDER log menu.
133+
| Show the CIDER log menu. Please note that this is considered advanced usage.
70134

71135
| `cider-log-framework`
72136
| kbd:[C-c M-l f]
@@ -79,16 +143,12 @@ the selected framework.
79143
| `cider-log-consumer`
80144
| kbd:[C-c M-l c]
81145
| Show the menu to manage consumers listening to log events.
82-
83-
| `cider-log-event`
84-
| kbd:[C-c M-l e]
85-
| Show the menu to manage log events.
86146
|===
87147

88148
== Log framework
89149

90150
CIDER Log Mode supports log frameworks that allow reconfiguration at
91-
run time. More specifically the framework should support attaching log
151+
runtime. More specifically the framework should support attaching log
92152
appenders to loggers, in order to capture events.
93153

94154
At the moment the following log frameworks are supported:
@@ -103,6 +163,15 @@ https://stackoverflow.com/a/17842174/12711900[difficulties] with
103163
configuration changes made at runtime, which are wiped out by the
104164
Log4j2 reconfiguration mechanism.
105165

166+
Timbre support is WIP as well.
167+
168+
TIP: If your logging framework of choice is not currently supported by CIDER Log Mode,
169+
you can opt to use Clojure's official `tools.logging` façade in your project, such that you can locally,
170+
unobstrusively tell it to use a supported framework (like Logback) instead of your project's default one.
171+
Note that `tools.logging`'s choice of logging backend implementation can be controlled with the
172+
`-Dclojure.tools.logging.factory` Java system property, which can be cleanly customized locally via Lein profiles,
173+
or Clojure CLI aliases.
174+
106175
=== Keybindings
107176

108177
|===

0 commit comments

Comments
 (0)