调用通用记录器的类名

时间:2017-11-27 12:15:13

标签: scala generics logging

我正在尝试为项目创建一个通用的Logger,但我无法在日志跟踪中获取调用类的名称。

我试图使用super.getClass获取调用类的名称,但它似乎不像我想的那样工作。 现在,这就是我所在的地方:

object GeneralLogger {

    val newLogger = LoggerFactory.getLogger(super.getClass)
    val configFile: Config = ConfigFactory.load()

    def SparkLogger(event: ErrorEvent) : Unit = {

      val sparkAppName: String = configFile.getString("spark.appName")
      val sparkMaster: String = configFile.getString("spark.master")
      val sparkCrossJoin: String = configFile.getString("spark.crossJoin")
      val sparkSqlWarehouse: String = configFile.getString("spark.sqlWarehouse")
      val sparkExecutorMemory: String = configFile.getString("spark.sparkExecutorMemory")

      newLogger.info("--- Spark Parameters ---")
      newLogger.info("sparkAppName: " + sparkAppName.toString)
      newLogger.info("sparkMaster: " + sparkMaster.toString)
      newLogger.info("sparkCrossJoin: " + sparkCrossJoin.toString)
      newLogger.info("sparkSqlWarehouse: " + sparkSqlWarehouse.toString)
      newLogger.info("sparkExecutorMemory: " + sparkExecutorMemory.toString)

    }

    def CassandraLogger() : Unit = {

      val cassandraHost: util.List[String] = configFile.getStringList("cassandra.host")
      val cassandraUser: String = configFile.getString("cassandra.username")
      val cassandraKeyspace: String = configFile.getString("cassandra.keyspaces.test")
      val cassandraPassword: String = configFile.getString("cassandra.password")

      newLogger.info("--- Cassandra Parameters ---")
      newLogger.info("cassandraHost: " + cassandraHost.toString)
      newLogger.info("cassandraUser: " + cassandraUser.toString)
      newLogger.info("cassandraKeyspace: " + cassandraKeyspace.toString)
      newLogger.info("cassandraPassword: " + cassandraPassword.toString)

    }

因此,此解决方案使我的日志如下所示:

2375 [main] INFO  c.g.bank.core.utils.GeneralLogger$ - --- Spark Parameters --- 
2449 [main] INFO  c.g.bank.core.utils.GeneralLogger$ - sparkAppName: Flap

我想要完成的是让调用类命名的日志,所以如果我在名为IncomeNotifier的类中实现GeneralLogger,我会以这种方式获取日志:

2375 [main] INFO  c.g.bank.core.utils.IncomeNotifier$ - --- Spark Parameters --- 
2449 [main] INFO  c.g.bank.core.utils.IncomeNotifier$ - sparkAppName: Flap 

提前感谢您的帮助

0 个答案:

没有答案