dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/models/log_entry.go (about)

     1  /*******************************************************************************
     2   * Copyright 2019 Dell Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
     5   * in compliance with the License. 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 distributed under the License
    10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    11   * or implied. See the License for the specific language governing permissions and limitations under
    12   * the License.
    13   *******************************************************************************/
    14  
    15  package models
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  
    21  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    22  )
    23  
    24  // These constants identify the log levels in order of increasing severity.
    25  const (
    26  	TraceLog = "TRACE"
    27  	DebugLog = "DEBUG"
    28  	InfoLog  = "INFO"
    29  	WarnLog  = "WARN"
    30  	ErrorLog = "ERROR"
    31  )
    32  
    33  type LogEntry struct {
    34  	Level         string        `bson:"logLevel,omitempty" json:"logLevel"`
    35  	Args          []interface{} `bson:"args,omitempty" json:"args"`
    36  	OriginService string        `bson:"originService,omitempty" json:"originService"`
    37  	Message       string        `bson:"message,omitempty" json:"message"`
    38  	Created       int64         `bson:"created,omitempty" json:"created"`
    39  	isValidated   bool          // internal member used for validation check
    40  }
    41  
    42  // UnmarshalJSON implements the Unmarshaler interface for the LogEntry type
    43  func (le *LogEntry) UnmarshalJSON(data []byte) error {
    44  	var err error
    45  	type Alias struct {
    46  		Level         *string       `json:"logLevel,omitempty"`
    47  		Args          []interface{} `json:"args,omitempty"`
    48  		OriginService *string       `json:"originService,omitempty"`
    49  		Message       *string       `json:"message,omitempty"`
    50  		Created       int64         `json:"created,omitempty"`
    51  	}
    52  	a := Alias{}
    53  	// Error with unmarshaling
    54  	if err = json.Unmarshal(data, &a); err != nil {
    55  		return err
    56  	}
    57  
    58  	// Nillable fields
    59  	if a.Level != nil {
    60  		le.Level = *a.Level
    61  	}
    62  	if a.OriginService != nil {
    63  		le.OriginService = *a.OriginService
    64  	}
    65  	if a.Message != nil {
    66  		le.Message = *a.Message
    67  	}
    68  	le.Args = a.Args
    69  	le.Created = a.Created
    70  
    71  	le.isValidated, err = le.Validate()
    72  
    73  	return err
    74  }
    75  
    76  // Validate satisfies the Validator interface
    77  func (le LogEntry) Validate() (bool, errors.EdgeX) {
    78  	if !le.isValidated {
    79  		logLevels := []string{TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog}
    80  		for _, name := range logLevels {
    81  			if name == le.Level {
    82  				return true, nil
    83  			}
    84  		}
    85  		return false, errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("Invalid level in LogEntry: %s", le.Level), nil)
    86  	}
    87  	return le.isValidated, nil
    88  }