Within this lesson, we will cover:

  • How to configure global logging.
  • How to configure per session logging.

The scripts and code for this lesson can be found within the Netmiko repo under the directory: examples/005_extras.

Netmiko Logging Intro

When debugging Netmiko, logging can be extremely useful. There are two ways in which logging can be enabled, at a global level or a per session level.

Per Session

To enable Netmiko logging to a file on a per connection level provide "session_log" : <filename> to your ConnectHandler like so:

device = {
    ...
    "session_log": 'netmiko_session.log',
}
   
ios_connection = ConnectHandler(**device)

This will write only the output for the current session to the logfile. At the point, a new session is created, the file will be overwritten.

Global

To enable logging for all connections to a file, we can import and use the logging module, as shown below. In this example, we enable debug level logging and direct all the logs to the file named netmiko.log.

import logging

logging.basicConfig(filename='netmiko_global.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

Once you have added the above and run Netmiko, you will see logs much like these:

DEBUG:netmiko:read_channel: 
leaf1-ios>
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:[find_prompt()]: prompt is leaf1-ios>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: leaf1\-ios
DEBUG:netmiko:_read_channel_expect read_data: 
DEBUG:netmiko:Pattern found: leaf1\-ios 
leaf1-ios>
DEBUG:netmiko:write_channel: b'enable\n'
DEBUG:netmiko:Pattern is: (leaf1\-ios|ssword)
DEBUG:netmiko:_read_channel_expect read_data: 
leaf1-ios>
DEBUG:netmiko:Pattern found: (leaf1\-ios|ssword) 
leaf1-ios>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: leaf1\-ios
DEBUG:netmiko:_read_channel_expect read_data: 
leaf1-ios>
DEBUG:netmiko:Pattern found: leaf1\-ios 
leaf1-ios>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: leaf1\-ios
DEBUG:netmiko:_read_channel_expect read_data: enable
Password: 
DEBUG:netmiko:_read_channel_expect read_data: 
leaf1-ios#
DEBUG:netmiko:Pattern found: leaf1\-ios enable
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜