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

Supporting log level for log-requests from configuration #20

Merged

Conversation

lambdadbmal
Copy link
Contributor

@lambdadbmal lambdadbmal commented Feb 14, 2023

Add new configuration feature to modify log-requests' log level.

You can modify requests log level in your config like ...

:duct.middleware.web/log-requests {:level :debug}

Also you can hide all requests log like ...

:duct.middleware.web/log-requests {:level :debug}
:duct.logger/timbre {:level :info}

Copy link
Contributor

@weavejester weavejester left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. This seems a reasonable change. I've reviewed the code and made some comments.

Comment on lines 15 to 19
(defn- log-request
([logger request]
(log-request logger request :info))
([logger request level]
(logger/log logger level ::request (select-keys request request-log-keys))))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a private function, so it doesn't need two different arities. Also take care to ensure that lines are indented correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for reviewing.
I delete older [logger request] arity. Now there is only `[logger request level]'.
I also reformatted my code with cljfmt.

(fn
([request]
(log-request logger request)
(log-request logger request (or level :info))
Copy link
Contributor

Choose a reason for hiding this comment

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

level is :info by default, so there's no need to check for nil at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you.
I removed (or ...) and change it to (log-request logger request level).

Comment on lines 35 to 36
(defn- logging-config [config]
{:duct.middleware.web/log-requests (or (:duct.middleware.web/log-requests config) {})
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the purpose of these changes. Why not just let the configurations merge normally? Why add this exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you.
That is my misunderstand of configuration.
Now I rollbacked original code.
And I added test code for changing log level via config in test/duct/module/web_test.clj.

@weavejester
Copy link
Contributor

Also, can you change the commit message to:

Add support for changing log level of requests

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from 425282c to 288762c Compare February 17, 2023 02:44
Comment on lines 15 to 17
(defn- log-request
([logger request level]
(logger/log logger level ::request (select-keys request request-log-keys))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the unnecessary (). e.g.

(defn- log-request [logger level request]
  (logger/log logger :info ::request (select-keys request request-log-keys)))

Comment on lines 106 to 107
(defmethod ig/init-key ::log-requests [_ {:keys [logger level]}]
#(wrap-log-requests % logger level))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a default for log levels here. e.g.

(defmethod ig/init-key ::log-requests
  [_ {:keys [logger level] :or {level :info}}]
  #(wrap-log-requests % logger level))

@@ -34,7 +34,7 @@

(def ^:private logging-config
{:duct.middleware.web/log-requests {}
:duct.middleware.web/log-errors {}
:duct.middleware.web/log-errors {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please ensure your diff doesn't include unnecessary spacing alterations.

Comment on lines 201 to 202
(is (= {:urlencoded true :keywordize false}
(:params (:duct.middleware.web/defaults config'))))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please ensure your diff doesn't include unnecessary spacing alterations.

Comment on lines 24 to 25
(let [logs (atom [])
handler (wrap-log-requests (constantly response) (->TestLogger logs) :trace)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure that the let clauses are correctly aligned, and the lines are 80 characters or under.

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from 943c43e to 2268e28 Compare February 17, 2023 04:01
@lambdadbmal
Copy link
Contributor Author

Thank you for reviewing again.
I fixed all of you mentioned.

Comment on lines 15 to 16
(defn- log-request
[logger request level]
(logger/log logger level ::request (select-keys request request-log-keys)))
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to put [logger request level] on a new line.

Comment on lines 24 to 26
(let [logs (atom [])
handler (wrap-log-requests (constantly response)
(->TestLogger logs) :trace)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Reformat according to style guidelines. i.e.

      (let [logs    (atom [])
            handler (wrap-log-requests (constantly response)
                                       (->TestLogger logs)
                                       :trace)]

{:duct.middleware.web/log-requests {:level :debug}})
config' (core/build-config config)]
(is (= {:level :debug}
(:duct.middleware.web/log-requests (:duct.middleware.web/defaults config'))))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure lines are within 80 characters.

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from 2268e28 to f6b1524 Compare February 17, 2023 08:19
@lambdadbmal
Copy link
Contributor Author

Thank you.
I fixed codes as you mentioned.
I also squashed all of commits.

Comment on lines 24 to 27
(let [logs (atom [])
handler (wrap-log-requests (constantly response)
(->TestLogger logs)
:trace)]
Copy link
Contributor

Choose a reason for hiding this comment

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

As per my last comment, could you fix the spacing in the let clause? There are 2 spaces after handler instead of 1. Otherwise it looks fine.

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from f6b1524 to 79dc5a0 Compare February 18, 2023 01:31
Copy link
Contributor

@weavejester weavejester left a comment

Choose a reason for hiding this comment

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

Hi, sorry for the delay. I've been busy working on fixing an issue in Ring. Now that I've come back to it, I've noticed that wrap-log-requests is a public function, and that we should probably use an option map instead, so we that don't have to change the API if we want to add any more functionality. Apologies for not thinking about that sooner.

(handler request respond raise))))
([handler logger]
(wrap-log-requests handler logger :info))
([handler logger level]
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking that to future-proof this API, it would make sense to make the third argument an options map. So instead of level, change it to {:keys [level] :or {level :info}}. Then in the ::log-requests method we can pass the options directly, minus the logger: (dissoc options :logger).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for you reply (and also contributing to Ring. We all clojurian thank a lot for your work).

I pushed new commit with your feedback.
I added third parameter to wrap-log-requests function as an option map.
And I revised test-wrap-log-requests to add a third parameter.
(We always call wrap-log-requests with three parameters. Is it correct?)

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from 79dc5a0 to dabd146 Compare March 1, 2023 00:58
@lambdadbmal lambdadbmal requested a review from weavejester March 1, 2023 01:03
@lambdadbmal
Copy link
Contributor Author

Hi, how's it going?
I would think you are still busy.
If you have a time to continue handling this PR, please let me know.
Take it easy.

Copy link
Contributor

@weavejester weavejester left a comment

Choose a reason for hiding this comment

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

Sorry for forgetting about this PR! Feel free to poke me whenever I take too long. I've looked it over, and I think after the change I've outlined it should be good to merge. Thanks for your work on this.


(defn- log-error [logger ex]
(logger/log logger :error ::handler-error ex))

(defn wrap-log-requests
"Log each request using the supplied logger. The logger must implement the
duct.core.protocols/Logger protocol."
[handler logger]
[handler logger {:keys [level] :or {level :info}}]
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add in a 2-arity version with a blank map? i.e.

(defn wrap-log-requests
  "..."
  ([handler logger]
    (wrap-log-requests handler logger {}))
  ([handler logger {:keys [level] :or {level :info}}]
    ...))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I will do it.

(testing "asynchronous"
(let [logs (atom [])
handler (wrap-log-requests
(fn [_ respond _] (respond response))
(->TestLogger logs))
(->TestLogger logs) {})
Copy link
Contributor

Choose a reason for hiding this comment

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

This {} can be removed when you have a default options argument.

@lambdadbmal lambdadbmal force-pushed the feature/add-logging-config branch from dabd146 to fe9a616 Compare March 16, 2023 04:33
@lambdadbmal lambdadbmal requested a review from weavejester March 16, 2023 04:34
@weavejester weavejester merged commit a5b61eb into duct-framework:master Mar 16, 2023
@weavejester
Copy link
Contributor

Thanks again for your work on this. Sorry it took so long to merge.

@lambdadbmal
Copy link
Contributor Author

Thank you for merging my PR.

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.

2 participants