{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/RecoHut-Projects/recohut/blob/master/nbs/utils/logging.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[K     |████████████████████████████████| 46 kB 1.6 MB/s \n",
      "\u001b[K     |████████████████████████████████| 56 kB 3.2 MB/s \n",
      "\u001b[K     |████████████████████████████████| 51 kB 222 kB/s \n",
      "\u001b[?25h  Building wheel for recohut (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
     ]
    }
   ],
   "source": [
    "#hide\n",
    "!pip install -q nbdev \n",
    "!pip install -q git+https://github.com/RecoHut-Projects/recohut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp utils.logging"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Logging\n",
    "> A logging utility module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from nbdev.showdoc import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "import os\n",
    "from pathlib import Path\n",
    "import logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def set_logger(log_path):\n",
    "    \"\"\"A Python module.\n",
    "\n",
    "    Args:\n",
    "        log_path (str): Path where you want to save the log file\n",
    "    \n",
    "    Examples:\n",
    "        ```python\n",
    "        set_logger('./model_a.log')\n",
    "        logging.info('Model A Logging Test Success')\n",
    "        ```\n",
    "    \"\"\"\n",
    "    log_dir = str(Path(log_path).parent)\n",
    "    if not os.path.exists(log_dir):\n",
    "        os.makedirs(log_dir)\n",
    "\n",
    "    # logs may not show in the file without the two lines\n",
    "    for handler in logging.root.handlers[:]: \n",
    "        logging.root.removeHandler(handler)\n",
    "        \n",
    "    logging.basicConfig(level=logging.INFO,\n",
    "                        format='%(asctime)s P%(process)d %(levelname)s %(message)s',\n",
    "                        handlers=[logging.FileHandler(log_path, mode='w'),\n",
    "                                  logging.StreamHandler()])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "test2 (__main__.TestLogger) ... 2021-12-05 09:34:23,519 P61 INFO Model A Logging Test Success\n",
      "ok\n",
      "testLogger (__main__.TestLogger) ... 2021-12-05 09:34:23,526 P61 INFO Model A Logging Test Success\n",
      "ok\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 2 tests in 0.013s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<unittest.main.TestProgram at 0x7ff784310e10>"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import unittest\n",
    "\n",
    "\n",
    "class TestLogger(unittest.TestCase):\n",
    "    def setUp(self):\n",
    "        set_logger('./model_a.log')\n",
    "        logging.info('Model A Logging Test Success')\n",
    "\n",
    "    def testLogger(self):\n",
    "        self.assertTrue(Path('./model_a.log').resolve().is_file())\n",
    "\n",
    "    def test2(self):\n",
    "        self.assertFalse(os.path.getsize('./model_a.log')==0)\n",
    "\n",
    "\n",
    "unittest.main(argv=[''], verbosity=2, exit=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2021-12-05 09:34:45\n",
      "\n",
      "Compiler    : GCC 7.5.0\n",
      "OS          : Linux\n",
      "Release     : 5.4.104+\n",
      "Machine     : x86_64\n",
      "Processor   : x86_64\n",
      "CPU cores   : 2\n",
      "Architecture: 64bit\n",
      "\n",
      "logging: 0.5.1.2\n",
      "IPython: 5.5.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#hide-input\n",
    "!pip install -q watermark\n",
    "%reload_ext watermark\n",
    "%watermark -a \"Sparsh A.\" -m -iv -u -t -d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
