how do I change log level in runtime without restarting spring boot application

后端 未结 10 1018
北荒
北荒 2020-12-23 19:55

I have deployed springboot application in PCF . I want to log the message based on the environment variable .What should I do so that the run time log level change will work

10条回答
  •  半阙折子戏
    2020-12-23 20:36

    You can also add a settings page in the web service to update the log level. This can then be done using ajax. The following example includes login and csrf token:

    First, add some form to specify the new log level. Can be improved for example by using a select element.

    Then, the request is sent:

    function submitLogLevelChange() {
        var className = document.getElementById('logClassName').value;
        var logLevel = document.getElementById("logLevel").value;
        $.ajax({
            // Set up security, see below.
            beforeSend: setHeader,
            type: 'POST',
            // specify the logger to be modified
            url: "/loggers/" + className,
            // specify the new log level
            data: '{"configuredLevel":"' + logLevel + '"}',
            contentType: 'application/json',
            processData: false,
            }).done(function(data, textStatus, jqXHR) {
                if (jqXHR.status === 200) {
                    // Happy
                } else if (jqXHR.status === 401) {
                    // Logged out or not enough user rights
                } else {
                    //Some other problem
                }
            })
            .fail(function(jqXHR, textStatus ) {
                if (jqXHR.status === 200) {
                    // Actually was successful, FireFox has some issues...
                } else {
                    // Failure
                }
            });
        }
    

    The following function injects the csrf token to the POST request:

    function setHeader(xhr) {
      var token = $("meta[name='_csrf']").attr("content");
      var header = $("meta[name='_csrf_header']").attr("content");
      xhr.setRequestHeader(header, token);
    }
    

提交回复
热议问题