github.com/ystia/yorc/v4@v4.3.0/plugin/log.go (about)

     1  // Copyright 2019 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plugin
    16  
    17  import (
    18  	stdlog "log"
    19  	"os"
    20  
    21  	hclog "github.com/hashicorp/go-hclog"
    22  	"github.com/ystia/yorc/v4/log"
    23  )
    24  
    25  // Creating a hclog logger so that Hashicorp go plugin logs can be correctly
    26  // parsed by Yorc Server parent process, and filtered appropriately,
    27  // according to the parent process log level.
    28  var hclogger = hclog.New(&hclog.LoggerOptions{
    29  	// Plugin logs have to go to stderr in order to be shown in the parent process
    30  	Output: os.Stderr,
    31  	Level:  hclog.Debug,
    32  	// Parent process expect child log in JSON format to be able to parse them
    33  	JSONFormat: true,
    34  })
    35  
    36  // Writer parsing log messages to infer the log level from message prefix
    37  // [DEBUG], [INFO], [WARN], [ERROR]
    38  var stdWriter = hclogger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true})
    39  
    40  // initPluginStdLog configures standard logs to use hclog in the plugin
    41  func initPluginStdLog() {
    42  	stdlog.SetOutput(stdWriter)
    43  	stdlog.SetPrefix("")
    44  	stdlog.SetFlags(0)
    45  }
    46  
    47  // initPluginYorcLog configures Yorc log to use hclog in the plugin
    48  func initPluginYorcLog() {
    49  	log.SetOutput(stdWriter)
    50  	log.SetPrefix("")
    51  	log.SetFlags(0)
    52  }