org.apache.log4j.spi.LoggingEvent Scala Examples

The following examples show how to use org.apache.log4j.spi.LoggingEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Stream.scala    From pulse   with Apache License 2.0 5 votes vote down vote up
package io.phdata.pulse

import io.phdata.pulse.log.{ HttpManager, JsonParser }
import monix.reactive.subjects.ConcurrentSubject
import monix.execution.Scheduler.Implicits.global
import monix.reactive.OverflowStrategy
import org.apache.log4j.helpers.LogLog
import org.apache.log4j.spi.LoggingEvent

import scala.concurrent.duration.FiniteDuration
import scala.util.{ Failure, Success, Try }

abstract class Stream[E](flushDuration: FiniteDuration, flushSize: Int, maxBuffer: Int) {

  val overflowStragegy = OverflowStrategy.DropNewAndSignal(maxBuffer, (_: Long) => None)
  val subject          = ConcurrentSubject.publish[E](overflowStragegy)

  subject
    .bufferTimedAndCounted(flushDuration, flushSize)
    .map(save)
    .subscribe()

  def append(value: E): Unit =
    Try { subject.onNext(value) } match {
      case Success(_) => ()
      case Failure(e) => LogLog.error("Error appending to stream", e)
    }

  def save(values: Seq[E])

}

class HttpStream(flushDuration: FiniteDuration,
                 flushSize: Int,
                 maxBuffer: Int,
                 httpManager: HttpManager)
    extends Stream[LoggingEvent](flushDuration, flushSize, maxBuffer) {

  val jsonParser = new JsonParser

  override def save(values: Seq[LoggingEvent]): Unit = {
    val logArray = values.toArray
    LogLog.debug(s"Flushing ${logArray.length} messages")
    val logMessage = jsonParser.marshallArray(logArray)

    httpManager.send(logMessage)
  }
} 
Example 2
Source File: SparkLogDivertAppender.scala    From multi-tenancy-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql.hive.thriftserver.multitenancy

import java.io.CharArrayWriter

import scala.collection.JavaConverters._

import org.apache.hadoop.hive.ql.session.OperationLog
import org.apache.log4j._
import org.apache.log4j.spi.{Filter, LoggingEvent}

import org.apache.spark.internal.Logging

class SparkLogDivertAppender extends WriterAppender with Logging {

  private var operationManager: ThriftServerOperationManager = _

  private class NameFilter(
       var operationManager: ThriftServerOperationManager) extends Filter {
    override def decide(ev: LoggingEvent): Int = {
      val log = operationManager.getOperationLog
      if (log == null) return Filter.DENY
      val currentLoggingMode = log.getOpLoggingLevel
      // If logging is disabled, deny everything.
      if (currentLoggingMode == OperationLog.LoggingLevel.NONE) return Filter.DENY
      Filter.NEUTRAL
    }
  }

  
  override protected def subAppend(event: LoggingEvent): Unit = {
    super.subAppend(event)
    // That should've gone into our writer. Notify the LogContext.
    val logOutput = writer.toString
    writer.reset()
    val log = operationManager.getOperationLog
    if (log == null) {
      logDebug(" ---+++=== Dropped log event from thread " + event.getThreadName)
      return
    }
    log.writeOperationLog(logOutput)
  }

} 
Example 3
Source File: RemoteAppender.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.worker.logging

import io.hydrosphere.mist.core.logging.LogEvent
import org.apache.log4j.spi.LoggingEvent
import org.apache.log4j.{AppenderSkeleton, Level, SimpleLayout}

class RemoteAppender(sourceId: String, logsWriter: LogsWriter) extends AppenderSkeleton {

  override def append(event: LoggingEvent): Unit = {

    val timeStamp = event.timeStamp
    val message = event.getRenderedMessage
    val evt = event.getLevel match {
      case Level.INFO => LogEvent.mkInfo(sourceId, message, timeStamp)
      case Level.DEBUG => LogEvent.mkDebug(sourceId, message, timeStamp)
      case Level.ERROR =>
        LogEvent.mkError(
          sourceId, message,
          Option(event.getThrowableInformation).map(_.getThrowable),
          timeStamp
        )
      case Level.WARN => LogEvent.mkWarn(sourceId, message, timeStamp)
      case _ => LogEvent.mkInfo(sourceId, this.getLayout.format(event), timeStamp)
    }
    logsWriter.write(evt)
  }

  override def close(): Unit = ()

  override def requiresLayout(): Boolean = true
}


object RemoteAppender {

  def create(sourceId: String, logsWriter: LogsWriter): RemoteAppender = {
    val jobLogsAppender = new RemoteAppender(sourceId, logsWriter)
    jobLogsAppender.setLayout(new SimpleLayout)
    jobLogsAppender.setName(sourceId)
    jobLogsAppender
  }

} 
Example 4
Source File: Log4jAppender.scala    From rollbar-scala   with MIT License 5 votes vote down vote up
package com.storecove.rollbar.appenders

import org.apache.log4j.helpers.LogLog
import org.apache.log4j.spi.{LoggingEvent, ThrowableInformation}
import org.apache.log4j.{AppenderSkeleton, Level}


class Log4jAppender extends AppenderSkeleton with AbstractAppender {

    override def append(event: LoggingEvent): Unit = {
        if (enabled) {
            try {
                logBuffer.enqueueFinite(this.layout.format(event).trim, limit)

                if (event.getLevel.isGreaterOrEqual(notifyLevel)) {
                    val hasThrowable = event.getThrowableInformation != null || event.getMessage.isInstanceOf[Throwable]
                    if (!onlyThrowable || hasThrowable) {
                        rollbarNotifier.notify(event.getLevel.toString, event.getMessage.toString, getThrowable(event), getMDCContext)
                    }
                }
            } catch {
                case e: Exception =>
                    val stackTrace = e.getStackTrace.map(trace => trace.toString).mkString("\n")
                    LogLog.error("error=" + e.getClass.getName + " with message=" + e.getMessage + "\n" + stackTrace)
            }
        }
    }

    override def requiresLayout(): Boolean = true

    override def close(): Unit = {}


    override def activateOptions(): Unit = {
        if (this.apiKey == null || this.apiKey.isEmpty) {
            println("No apiKey set for the appender named [" + getName + "].")
        } else if (this.environment == null || this.environment.isEmpty) {
            println("No environment set for the appender named [" + getName + "].")
        } else {
            println(s"PARAMETERS SET\n\n$apiKey / $environment\n")
            super.activateOptions()
        }
    }

    protected def getThrowable(event: LoggingEvent): Option[Throwable] = {
        event.getThrowableInformation match {
            case throwableInfo: ThrowableInformation => Some(throwableInfo.getThrowable)
            case _ => event.getMessage match {
                case throwable: Throwable => Some(throwable)
                case _ => None
            }
        }
    }

    override protected def notifyLevel: Level = Level.toLevel(notifyLevelString)

    def setNotifyLevel(notifyLevel: String): Unit = notifyLevelString = notifyLevel
}