{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import panel as pn\n",
    "pn.extension('terminal')\n",
    "pn.config.console_output = 'disable'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Debugger` is an uneditable Card layout which lets your front end show logs and errors that may fire whenever your dashboard is running.\n",
    "\n",
    "When running a panel server with several end users, the debugger will let them know whenever one of their interractions did not run as planned. Information can also be logged for end users to know that everything went well in the back-end. If logger_names is not specified, events must be logged using the `panel` logger or a custom child logger, e.g. `panel.myapp`.\n",
    "\n",
    "It is also usable in a live notebook and complements the `console_output` logs.\n",
    "\n",
    "Note the debugger is based on the [terminal widget](Terminal.ipynb) and requires `pn.extension('terminal')` to be called."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  Parameters:\n",
    "\n",
    "\n",
    "* **``only_last``** (bool): when exceptions are logged, indicates whether only the last trace in the stack will be prompted. Note this does not change how logs are thrown to `stderr`, the logs on server side will be accessible as you programmed them. Default: `False`\n",
    "* **``level``** (int): The log level you want to be prompt on the front end. Available values are the same as in [the standard levels of the `logging` package](https://docs.python.org/3/library/logging.html#levels). Default: `logging.ERROR`\n",
    "* **``formatter_args``** (dict): arguments to pass to the [Formatter object](https://docs.python.org/3/library/logging.html#formatter-objects). You may modify the prompt format with `fmt` or date formats with `datefmt` as in the standard library. Default: `{'fmt':\"%(asctime)s [%(name)s - %(levelname)s]: %(message)s\"}`\n",
    "* **``logger_names``** (list): [loggers called with `getLogger`](https://docs.python.org/3/library/logging.html#logging.getLogger) which will be prompted to the terminal. In a server context, note only the errors thrown  within the user session will be prompted, not all errors throughout the server. Default: `['panel']`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "debug = pn.widgets.Debugger(name='My Debugger')\n",
    "debug"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are making a radio button attached to a callback that intentionaly throws an error. Upon clicking on the error generating buttons, you will see the error number increasing in the debugger. Note the inline log was disabled with `pn.config.console_output = 'disable'` to avoid cluterring the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "btn = pn.widgets.RadioButtonGroup(name='Throw error', value='no error', options=['ZeroDivision', 'no error', 'Custom error'], button_type='danger')\n",
    "\n",
    "@pn.depends(btn)\n",
    "def throw_error(event):\n",
    "    if event == 'ZeroDivision':\n",
    "        return pn.pane.Str(1/0)\n",
    "    elif event == 'no error':\n",
    "        return pn.pane.Str('Hello!')\n",
    "    elif event == 'Custom error':\n",
    "        raise Exception('custom error thrown')\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pn.Column(btn, throw_error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We may also send information to the front end. Let's create a new debugger with a lower logging level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = logging.getLogger('panel.myapp')\n",
    "debug_info = pn.widgets.Debugger(name='Debugger info level', level=logging.INFO, sizing_mode='stretch_both',\n",
    "                                logger_names=['panel.myapp'],#comment this line out to get all panel errors\n",
    "                                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "btn_info = pn.widgets.RadioButtonGroup(name='show info', options=['debug', 'info', 'warning'])\n",
    "\n",
    "@pn.depends(btn_info)\n",
    "def throw_error(event):\n",
    "    msg = (event + ' sent from btn_info').capitalize()\n",
    "    if event == 'info':\n",
    "        logger.info(msg)\n",
    "    elif event == 'debug':\n",
    "        logger.debug(msg)\n",
    "    elif event == 'warning':\n",
    "        logger.warning(msg)\n",
    "    return msg\n",
    "c = pn.Column(btn_info, debug_info, throw_error, sizing_mode='stretch_both')\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The end user may save the logs by clicking on the floppy disk 💾 and clear the debugger by clicking on the check box button ☐."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "debug_info.btns"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
