spark代码中添加logger_使用Log4j在日志中输出Spark应用程序ID

I have a custom Log4j file for the Spark application. I would like to output Spark app id along with other attributes like message and date so the JSON string structure would look like this:

{"name":,"time":,"date":,"level":,"thread":,"message":,"app_id":}

Now, this structure looks like this:

{"name":,"time":,"date":,"level":,"thread":,"message":}

How can I define such layout for the Spark driver logs?

My log4j file looks like this:

解决方案

I doubt that org.apache.hadoop.log.Log4Json can be adjusted for this purpose. According to its javadoc and source code it might be rather cumbersome.

Although it looks like you are using Log4j 1x, its API is quite flexible and we can easily define our own layout by extending org.apache.log4j.Layout.

We'll need a case class that will be transformed into JSON according to the target structure:

case class LoggedMessage(name: String,

appId: String,

thread: String,

time: Long,

level: String,

message: String)

And Layout might be extended as follows. To access the value of "app_id", we'll use Log4j's Mapped Diagnostic Context

import org.apache.log4j.Layout

import org.apache.log4j.spi.LoggingEvent

import org.json4s.DefaultFormats

import org.json4s.native.Serialization.write

class JsonLoggingLayout extends Layout {

// required by the API

override def ignoresThrowable(): Boolean = false

// required by the API

override def activateOptions(): Unit = { /* nothing */ }

override def format(event: LoggingEvent): String = {

// we are using json4s for JSON serialization

implicit val formats = DefaultFormats

// retrieve app_id from Mapped Diagnostic Context

val appId = event.getMDC("app_id") match {

case null => "[no_app]" // logged messages outside our app

case defined: AnyRef => defined.toString

}

val message = LoggedMessage("TODO",

appId,

Thread.currentThread().getName,

event.getTimeStamp,

event.getLevel.toString,

event.getMessage.toString)

write(message) + "\n"

}

}

Finally, when the Spark session is created, we put the app_id value into MDC:

import org.apache.log4j.{Logger, MDC}

// create Spark session

MDC.put("app_id", session.sparkContext.applicationId)

logger.info("-------- this is info --------")

logger.warn("-------- THIS IS A WARNING --------")

logger.error("-------- !!! ERROR !!! --------")

This produces following logs:

{"name":"TODO","appId":"local-1550247707920","thread":"main","time":1550247708149,"level":"INFO","message":"-------- this is info --------"}

{"name":"TODO","appId":"local-1550247707920","thread":"main","time":1550247708150,"level":"WARN","message":"-------- THIS IS A WARNING --------"}

{"name":"TODO","appId":"local-1550247707920","thread":"main","time":1550247708150,"level":"ERROR","message":"-------- !!! ERROR !!! --------"}

And, of course, do not forget to refer the implementation in log4j config xml:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值