当前位置: 首页>>代码示例>>Golang>>正文


Golang Logger.Warn方法代码示例

本文整理汇总了Golang中github.com/Sirupsen/logrus.Logger.Warn方法的典型用法代码示例。如果您正苦于以下问题:Golang Logger.Warn方法的具体用法?Golang Logger.Warn怎么用?Golang Logger.Warn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/Sirupsen/logrus.Logger的用法示例。


在下文中一共展示了Logger.Warn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: sendCloudFormationResponse

func sendCloudFormationResponse(customResourceRequest *AbstractCustomResourceRequest,
	results map[string]interface{},
	responseErr error,
	logger *logrus.Logger) error {

	parsedURL, parsedURLErr := url.ParseRequestURI(customResourceRequest.ResponseURL)
	if nil != parsedURLErr {
		return parsedURLErr
	}

	status := "FAILED"
	if nil == responseErr {
		status = "SUCCESS"
	}
	reasonText := ""
	if nil != responseErr {
		reasonText = fmt.Sprintf("%s. Details in CloudWatch Logs: %s : %s",
			responseErr.Error(),
			customResourceRequest.LogGroupName,
			customResourceRequest.LogStreamName)
	} else {
		reasonText = fmt.Sprintf("Details in CloudWatch Logs: %s : %s",
			customResourceRequest.LogGroupName,
			customResourceRequest.LogStreamName)
	}

	responseData := map[string]interface{}{
		"Status":             status,
		"Reason":             reasonText,
		"PhysicalResourceId": customResourceRequest.PhysicalResourceID,
		"StackId":            customResourceRequest.StackID,
		"RequestId":          customResourceRequest.RequestID,
		"LogicalResourceId":  customResourceRequest.LogicalResourceID,
	}
	if nil != responseErr {
		responseData["Data"] = map[string]interface{}{
			"Error": responseErr,
		}
	} else if nil != results {
		responseData["Data"] = results
	} else {
		responseData["Data"] = map[string]interface{}{}
	}

	logger.WithFields(logrus.Fields{
		"ResponsePayload": responseData,
	}).Debug("Response Info")

	jsonData, jsonError := json.Marshal(responseData)
	if nil != jsonError {
		return jsonError
	}

	responseBuffer := strings.NewReader(string(jsonData))
	req, httpErr := http.NewRequest("PUT", customResourceRequest.ResponseURL, responseBuffer)

	if nil != httpErr {
		return httpErr
	}
	// Need to use the Opaque field b/c Go will parse inline encoded values
	// which are supposed to be roundtripped to AWS.
	// Ref: https://tools.ietf.org/html/rfc3986#section-2.2
	// Ref: https://golang.org/pkg/net/url/#URL
	req.URL = &url.URL{
		Scheme:   parsedURL.Scheme,
		Host:     parsedURL.Host,
		Opaque:   parsedURL.RawPath,
		RawQuery: parsedURL.RawQuery,
	}
	logger.WithFields(logrus.Fields{
		"URL": req.URL,
	}).Debug("Created URL response")

	// Although it seems reasonable to set the Content-Type to "application/json" - don't.
	// The Content-Type must be an empty string in order for the
	// AWS Signature checker to pass.
	// Ref: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html
	req.Header.Set("Content-Type", "")

	client := &http.Client{}
	resp, httpErr := client.Do(req)
	if httpErr != nil {
		return httpErr
	}
	logger.WithFields(logrus.Fields{
		"LogicalResourceId":  customResourceRequest.LogicalResourceID,
		"Result":             responseData["Status"],
		"ResponseStatusCode": resp.StatusCode,
	}).Info("Sent CloudFormation response")

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		body, bodyErr := ioutil.ReadAll(resp.Body)
		if bodyErr != nil {
			logger.Warn("Unable to read body: " + bodyErr.Error())
			body = []byte{}
		}
		return fmt.Errorf("Error sending response: %d. Data: %s", resp.StatusCode, string(body))
	}
	defer resp.Body.Close()
	return nil
//.........这里部分代码省略.........
开发者ID:mweagle,项目名称:cloudformationresources,代码行数:101,代码来源:cloudFormationResources.go


注:本文中的github.com/Sirupsen/logrus.Logger.Warn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。