Indices and tablesĀ¶. Module Index. PyMISP Navigation. README; PyMISP - Python Library to access MISP; Related Topics. Documentation overview. Import logging logger = logging. GetLogger ('pymisp') # Configure it as you whish, for example, enable DEBUG mode: logger. SetLevel (logging. DEBUG) Or if you want to write the debug output to a file instead of stderr: import pymisp import logging logger = logging. GetLogger ('pymisp') logging. BasicConfig (level = logging.
- # -*- coding: utf-8 -*-
- import string
- from keys import misp_url, misp_key
- # For python2 & 3 compat, a bit dirty, but it seems to be the least bad one
- input = raw_input
- pass
- return PyMISP(url, key, True, 'json', debug=True)
- if __name__ '__main__':
- parser = argparse.ArgumentParser(description='Create an event on MISP.')
- parser.add_argument('-f', '--file', help='The bro log file to upload')
- file_contents = input_file.read()
- entries = []
- fields = ['ts', 'uid', 'id.orig_h', 'id.orig_p', 'id.resp_h',
- 'id.resp_p', 'fuid', 'file_mime_type', 'file_desc',
- 'seen.indicator', 'seen.indicator_type', 'seen.where',
- for line in file_contents:
- entry = {}
- for i in range(0, len(fields)):
- entries.append(entry)
- if len(entries) > 0:
- event = misp.new_event('0', '1', '0', 'Event based on ' + args.file)
- url = entry['seen.indicator']
- ip_port = entry['id.resp_h'] + '|' + entry['id.resp_p']
- event = misp.add_named_attribute(event, 'url', url)
- event = misp.add_named_attribute(event, 'ip-dst|port', ip_port)
- print('Event added with', len(entries), 'attributes.')
Write your code with a nice logger
works as expected. Logging using
logger.debug('Hello! :)')
logs to file and console.In a second step you want to import an external module which is also logging using logging module:
- Install it using
pip3 install pymisp
(or any other external module) - Import it using
from pymisp import PyMISP
(or any other external module) - Create an object of it using
self.pymisp = PyMISP(self.ds_model.api_url, self.ds_model.api_key, False, 'json')
(or any other...)
What now happens is, that every debug log output from the imported module is getting logged to the log file and the console. The question now is, how to set a different (higher) log level for the imported module.
gies0rgies0r
2 Answers
Most third party modules that use logging implement a logger and store it in a variable called
logger
or _log
at module level. This means you can extract the logger and adapt it to your needs. In your case:F.M.F.F.M.F.
A colleague of mine helped with this question:
- Get a named logger
yourLogger = logging.getLogger('your_logger')
- Add a filter to each handler prevents them to print/save other logs than yours
![Pymisp Import Stix Pymisp Import Stix](/uploads/1/2/5/0/125015867/554882966.png)
gies0rgies0r