要添加自定义属性,请调用 slf4j 流畅 API 的方法,例如 Logger.atInfo 和 Logger.atError。使用这些方法在日志条目中设置键值对。每个返回一个 org.slf4j.spi.LoggingEventBuilder (https://www.slf4j.org/apidocs/org/slf4j/spi/LoggingEventBuilder.html),您可以用它来设置日志消息。
以下示例中的代码将消息“Logging with attributes”记录到事件表的 VALUE 列中。它还向 RECORD_ATTRIBUTES 列添加了一个自定义属性。
CREATEORREPLACEPROCEDUREdo_logging_scala()RETURNSVARCHARLANGUAGESCALARUNTIME_VERSION='2.12'PACKAGES=('com.snowflake:telemetry:latest','com.snowflake:snowpark_2.12:latest')HANDLER='ScalaLoggingHandler.doThings'AS$$importorg.slf4j.Loggerimportorg.slf4j.LoggerFactoryimportcom.snowflake.snowpark.SessionclassScalaLoggingHandler{privatevallogger:Logger=LoggerFactory.getLogger(getClass)defdoThings(session:Session):String={logger.atInfo().addKeyValue("custom1","value1").setMessage("Logging with attributes").log();return"SUCCESS"}}$$;
CREATEORREPLACEPROCEDUREdo_logging_scala()RETURNSVARCHARLANGUAGESCALARUNTIME_VERSION='2.13'PACKAGES=('com.snowflake:telemetry:latest','com.snowflake:snowpark_2.13:latest')HANDLER='ScalaLoggingHandler.doThings'AS$$importorg.slf4j.Loggerimportorg.slf4j.LoggerFactoryimportcom.snowflake.snowpark.SessionclassScalaLoggingHandler{privatevallogger:Logger=LoggerFactory.getLogger(getClass)defdoThings(session:Session):String={logger.atInfo().addKeyValue("custom1","value1").setMessage("Logging with attributes").log();return"SUCCESS"}}$$;
CREATEORREPLACEPROCEDUREdo_logging()RETURNSVARCHARLANGUAGESCALARUNTIME_VERSION='2.12'PACKAGES=('com.snowflake:snowpark_2.12:latest','com.snowflake:telemetry:latest')HANDLER='ScalaLoggingHandler.doThings'AS$$importorg.slf4j.Loggerimportorg.slf4j.LoggerFactoryimportcom.snowflake.snowpark.SessionclassScalaLoggingHandler{privatevallogger:Logger=LoggerFactory.getLogger(getClass)logger.info("Logging from within the Scala constructor.")defdoThings(session:Session):String={logger.info("Logging from Scala method start.")try{throwException}catch{casee:Exception=>logger.error("Logging an error from Scala handler: "+e.getMessage())return"ERROR"}return"SUCCESS"}// Simulate a thrown exception to catch.@throws(classOf[Exception])privatedefthrowException={thrownewException("Something went wrong.")}}$$;
CREATEORREPLACEPROCEDUREdo_logging()RETURNSVARCHARLANGUAGESCALARUNTIME_VERSION='2.13'PACKAGES=('com.snowflake:snowpark_2.13:latest','com.snowflake:telemetry:latest')HANDLER='ScalaLoggingHandler.doThings'AS$$importorg.slf4j.Loggerimportorg.slf4j.LoggerFactoryimportcom.snowflake.snowpark.SessionclassScalaLoggingHandler{privatevallogger:Logger=LoggerFactory.getLogger(getClass)logger.info("Logging from within the Scala constructor.")defdoThings(session:Session):String={logger.info("Logging from Scala method start.")try{throwException}catch{casee:Exception=>logger.error("Logging an error from Scala handler: "+e.getMessage())return"ERROR"}return"SUCCESS"}// Simulate a thrown exception to catch.@throws(classOf[Exception])privatedefthrowException={thrownewException("Something went wrong.")}}$$;
---------------------------------------------------------------------------| SEVERITY | MESSAGE |---------------------------------------------------------------------------| "INFO" | "Logging from within the Scala constructor." |---------------------------------------------------------------------------| "INFO" | "Logging from Scala method start." |---------------------------------------------------------------------------| "ERROR" | "Logging an error from Scala handler: Something went wrong." |---------------------------------------------------------------------------