<commit_before>(ns ^{:doc "Conigure and change the log level of the log4j2 system."
      :author "<NAME>"}
    zensols.actioncli.log4j2
  (:require [clojure.tools.logging :as log]
            [clojure.java.io :as io])
  (:import (org.apache.logging.log4j LogManager Level)
           (org.apache.logging.log4j.core.config Configurator
                                                 ConfigurationSource)))

(defn change-log-level
  "Change the Log4j2 log level.

  **level-thing** the log level as either the Level instance or a string."
  [level-thing]
  (log/debugf "changing log level to %s" level-thing)
  (let [ctx (LogManager/getContext false)
        config (.getConfiguration ctx)
        log-config (.getLoggerConfig config LogManager/ROOT_LOGGER_NAME)
        loggers (concat (.values (.getLoggers config))
                        [log-config])
        level (if (string? level-thing)
                (Level/toLevel level-thing)
                level-thing)]
    (.setLevel log-config level)
    (doseq [logger loggers]
      (.setLevel logger level))
    (.updateLoggers ctx)))

(defn configure<commit_msg>troubleshoot log4j2 change log level<commit_after>(ns ^{:doc "Conigure and change the log level of the log4j2 system."
      :author "<NAME>"}
    zensols.actioncli.log4j2
  (:require [clojure.tools.logging :as log]
            [clojure.tools.logging.impl :as i]
            [clojure.java.io :as io])
  (:import (org.apache.logging.log4j LogManager Level)
           (org.apache.logging.log4j.core.config Configurator
                                                 ConfigurationSource)
           (org.apache.logging.log4j.core.config LoggerConfig)
           (com.zensols.log LogUtil)
           ))

(defn change-log-level
  "Change the Log4j2 log level.

  **level-thing** the log level as either the Level instance or a string."
  [level-thing]
  ;(log/debugf "changing log level to %s" level-thing)
  (let [ctx (LogManager/getContext
             ;(.getClassLoader (.getClass LogManager))
             ;;(.getClassLoader (.getClass *ns*))
             ;(.getClassLoader clojure.lang.RT)
             false)
        config (.getConfiguration ctx)
        log-config (.getLoggerConfig config LogManager/ROOT_LOGGER_NAME
                                     )
        loggers (concat (.values (.getLoggers config))
                        ;[log-config]
                        )
        level (if (string? level-thing)
                (Level/toLevel level-thing)
                level-thing)]
    (println "BEF " log-config "::" (.getName log-config) "::" (.getLevel log-config))
    (.setLevel log-config level)
    (println "AFT " log-config "::" (.getName log-config) "::" (.getLevel log-config))
    ;(.debug log-config "TEST")
    (doseq [logger loggers]
      (println "BEF " logger "::" (.getName logger))
      (org.apache.logging.log4j.LogManager/getLogger (.getName logger))
      (.setLevel logger level)
      (println "AFT " logger "::" (.getName logger))
      (org.apache.logging.log4j.LogManager/getLogger (.getName logger))
      )
    (println ctx)
    ;(Configurator/setAllLevels "zensols.actioncli" (Level/toLevel "trace"))
    (.updateLoggers ctx)
    ))

;;(LogUtil/setLevel "zensols.actioncli.log4j2" )
;; (->> (macroexpand '(log/error "INF"))
;;      clojure.pprint/pprint)

;; (-> (i/get-logger log/*logger-factory* "zensols.actioncli.log4j2")
;;      (.setLevel (Level/toLevel "trace")))

;(enabled? (get-logger (log4j2-factory) "zensols.actioncli") :error)

;(log/error "ERR")
;(log/info "INF")
;(log/debug "DEB--------------------------------------------------")
;(log/trace "TRA--------------------------------------------------")

;(with-open [in (io/input-stream (io/file "resources/log4j2.xml"))] (LogUtil/config in))
;(LogUtil/setAllLevel (Level/toLevel "info"))

;; (change-log-level "trace")

;; (LogUtil/setLevel "zensols" (Level/toLevel "info"))
;; (LogUtil/test)
;(tmp)

;; (org.apache.logging.log4j.LogManager/getLogger "a")
;; (org.apache.logging.log4j.LogManager/getRootLogger)
;; (org.apache.logging.log4j.LogManager/ROOT_LOGGER_NAME)
;; (org.apache.logging.log4j.LogManager/getLogger "zensols.actioncli.log4j2")


;(clojure.tools.logging.impl/find-factory)

;; (.setLevel (org.apache.logging.log4j.LogManager/getLogger "zensols.actioncli")
;;            (Level/toLevel "trace"))


(defn- tmp []
  (with-open [in (io/input-stream (io/file "test-resources/log4j2.xml"))]
    (->> (ConfigurationSource. in)
         (Configurator/initialize nil))))

(defn configure
{'fraction_non_alphanumeric': 0.16500994035785288, 'fraction_numerical': 0.008394079964656506, 'mean_word_length': 2.43030303030303, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4216142', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1422, 'n_words': 309}