package com.chenyang.druid.wall;

import com.chenyang.druid.support.json.JSONUtils;
import com.chenyang.druid.support.logging.Log;
import com.chenyang.druid.support.logging.LogFactory;
import java.util.Map;
import java.util.Properties;

public class WallProviderStatLoggerImpl extends WallProviderStatLoggerAdapter implements WallProviderStatLogger {
   private static Log LOG = LogFactory.getLog(WallProviderStatLoggerImpl.class);
   private Log logger;

   public WallProviderStatLoggerImpl() {
      this.logger = LOG;
   }

   public void configFromProperties(Properties properties) {
      String property = properties.getProperty("druid.stat.loggerName");
      if (property != null && property.length() > 0) {
         this.setLoggerName(property);
      }

   }

   public void setLoggerName(String loggerName) {
      this.logger = LogFactory.getLog(loggerName);
   }

   public void setLogger(Log logger) {
      if (logger == null) {
         throw new IllegalArgumentException("logger can not be null");
      } else {
         this.logger = logger;
      }
   }

   public boolean isLogEnable() {
      return this.logger.isInfoEnabled();
   }

   public void log(String value) {
      this.logger.info(value);
   }

   public void log(WallProviderStatValue statValue) {
      if (this.isLogEnable()) {
         Map<String, Object> map = statValue.toMap();
         String text = JSONUtils.toJSONString(map);
         this.log(text);
      }
   }
}
