(ns log-levels
(:require [clojure.string :as str]))
(defn parts
"Divide a text into there parts with regex"
[s]
(re-find #"\[<(.*)>\]: <(.*)>" s));
(defn message
"Takes a string representing a log line
and returns its message with whitespace trimmed."
[s]
(str/trim (last (str/split s #":"))))
(defn log-level
"Takes a string representing a log line
and returns its level in lower-case."
[s]
(parts s))
but why is the log-level given nill instead of someting like this ["error', "you are a fool"]
when I call it like this '(loglevel [“error” : “you are a fool”])
In your example of how you call log-level, you pass it a vector. But it must be a string to work properly.
Also note that even if you pass it "error: you're a fool", it still won’t match because that RegEx requires both the square and the angle brackets, and the latter must go around both the level and the message.
I’d suggest building things from smaller to larger while constantly making sure that everything works in a REPL. E.g. I would test that re-find on some message immediately upon coming up with a RegEx that I think should work.
At one way I thought I needed them.
This is solving the exercism problem
(ns log-levels
(:require [clojure.string :as str]))
(defn parts
"Divide a text into there parts with regex"
[s]
(re-find #"\[(.*)\]: (.*)" s));
(defn message
"Takes a string representing a log line
and returns its message with whitespace trimmed."
[s]
(str/trim (nth (parts s) 2 )))
(defn log-level
"Takes a string representing a log line
and returns its level in lower-case."
[s]
(str/lower-case (nth (parts s) 1 )))
(defn reformat
"Takes a string representing a log line and formats it
with the message first and the log level in parentheses."
[s]
(str (message s) " (" (log-level s) ")"))
` ``
Thanks a lot