로그 내보내기

Cloud Code 스크립트모듈에서 로그를 내보낼 수 있습니다.

Cloud Code C# 모듈

Microsoft.Extensions.Logging 라이브러리를 사용하여 Cloud Code C# 모듈에서 로그를 내보낼 수 있습니다. Cloud Code는 종속성 삽입을 통해 ILogger를 요청할 때마다 코드에 UGS Logger를 자동으로 제공합니다.

다음 코드 샘플은 C# 모듈을 설정하는 데 필요한 모든 사항을 보여 줍니다.

C#

using Microsoft.Extensions.Logging;
using Unity.Services.CloudCode.Core;

namespace CloudCodeModules;

public class LoggingExample
{
    // Setting up the Dependency Injection. Cloud Code will inject a UGS Logger
    // into "_logger" which can then be used by all methods in this class
    private readonly ILogger<LoggingExample> _logger;
    public LoggingExample(ILogger<LoggingExample> logger)
    {
        _logger = logger;
    }

    [CloudCodeFunction("SayHello")]
    public string SayHello(IExecutionContext ctx, string name)
    {
        // Logging out a standard message
        _logger.LogDebug("received a request to SayHello");

        // Logging out a templated message. The template variable "name"
        // will be available as a key-value pair inside the log attributes map.
        // Templated keys cannot use dot notation for namespacing.
        _logger.LogInformation("saying hello to {name}", name);

        // A scope can be used to share sets of attributes between multiple log
        // entries. Attribute keys in a scope can use dot notation for namespacing
        // purposes.
        using (_logger.BeginScope(new Dictionary<string, object>
            {
                {"example.someInteger", 7 },
                {"example.someString", "frog blast the vent core!" }
            }))
        {
            _logger.LogInformation("logging within a scope");
            _logger.LogInformation("logging again with the same attributes");
        }

        return $"Hello, {name}!";
    }
}

Cloud Code JavaScript 스크립트

스크립트 컨텍스트의 일부로 자동 제공되는 기본 로거를 사용하여 Cloud Code JavaScript 스크립트에서 로그를 내보낼 수 있습니다.

다음 코드 샘플은 JavaScript 스크립트를 설정하는 데 필요한 모든 사항을 보여 줍니다.

JavaScript

module.exports = async ({ params, context, logger }) => {
    logger.info("script executing", {"example.someString": "frog blast the vent core!"});
    logger.warning("this is a serious warning that the cheese is about to run out.");
    logger.error("out of cheese :(", {"example.foo": 42}, {"example.bar": 7});
};

이 로거는 다음과 같은 메서드를 제공합니다.

  • debug(message, ...logAttributes)
  • info(message, ...logAttributes)
  • warning(message, ...logAttributes)
  • error(message, ...logAttributes)
  • fatal(message, ...logAttributes)

제한 사항

호출별로 여러 개의 로그 속성을 전달할 수 있지만, 다음과 같은 제한 사항이 적용됩니다.

  • 각 로그 속성은 단일 키/값 페어가 있는 오브젝트여야 합니다.
  • 값은 비어 있지 않은 기본 값이어야 합니다(false 부울은 허용됨).