Logger.java

  1. package org.drip.analytics.support;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>Logger</i> implements level-set logging, backed by either the screen or a file. Logging always includes
  84.  * time stamps, and happens according to the level requested.
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  90.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/support/README.md">Assorted Support and Helper Utilities</a></li>
  92.  *  </ul>
  93.  *
  94.  * @author Lakshmi Krishnamurthy
  95.  */

  96. public class Logger {

  97.     /**
  98.      * Logger level ERROR
  99.      */

  100.     public static final int ERROR = 0;

  101.     /**
  102.      * Logger level WARNING
  103.      */

  104.     public static final int WARNING = 1;

  105.     /**
  106.      * Logger level INFO
  107.      */

  108.     public static final int INFO = 2;

  109.     /**
  110.      * Logger level DEBUG
  111.      */

  112.     public static final int DEBUG = 4;

  113.     private static boolean s_bInit = false;
  114.     private static java.io.BufferedWriter _writeLog = null;

  115.     private static final java.lang.String ImprintPreSub (
  116.         final int iLevel,
  117.         final java.lang.String strMsg)
  118.     {
  119.         if (null == strMsg || strMsg.isEmpty()) return "";

  120.         java.lang.StringBuilder sb = new java.lang.StringBuilder();

  121.         sb.append ("[").append ((new java.util.Date()).toString()).append ("|Level:").append (iLevel).append
  122.             ("|").append (strMsg).append ("]\n");

  123.         return sb.toString();
  124.     }

  125.     /**
  126.      * Initialize the logger from a configuration file
  127.      *
  128.      * @param strConfigFile Configuration file containing the logger file location
  129.      *
  130.      * @return boolean indicating whether initialization succeeded
  131.      */

  132.     public static boolean Init (
  133.         final java.lang.String strConfigFile)
  134.     {
  135.         if (null == strConfigFile || strConfigFile.isEmpty()) return true;

  136.         if (s_bInit) return true;

  137.         try {
  138.             _writeLog = new java.io.BufferedWriter (new java.io.FileWriter
  139.                 (org.drip.param.config.ConfigLoader.LoggerLocation (strConfigFile)));

  140.             return s_bInit = true;
  141.         } catch (java.lang.Exception e) {
  142.             e.printStackTrace();
  143.         }

  144.         return s_bInit = false;
  145.     }

  146.     /**
  147.      * Log a specific message to the level
  148.      *
  149.      * @param iLevel the level of message (ERROR/WARNING/INFO/DEBUG)
  150.      * @param bHardLog whether the logging is to file/DB (true) or to screen (false)
  151.      * @param strMsg Message to be logged
  152.      *
  153.      * @return boolean indicating whether logging operation succeeded
  154.      */

  155.     public static boolean Log (
  156.         final int iLevel,
  157.         final boolean bHardLog,
  158.         final java.lang.String strMsg)
  159.     {
  160.         if (!s_bInit || null == strMsg || strMsg.isEmpty()) return false;

  161.         if (bHardLog) {
  162.             try {
  163.                 _writeLog.write (ImprintPreSub (iLevel, strMsg));

  164.                 return true;
  165.             } catch (java.lang.Exception e) {
  166.                 e.printStackTrace();
  167.             }

  168.             return false;
  169.         }

  170.         System.out.println (ImprintPreSub (iLevel, strMsg));

  171.         return true;
  172.     }
  173. }