Formatters

Formatters create a data structure and applies data into it.

Formatters are classes that contain the logic of how to format your data. They receive data and a format based on your configuration or fake data for you.

Developing new formatters is as easy as as baking a pop-tart. All you have to do is implement a new class and provide your format and data structures. You can check the Apache Access Log implementation as a reference.

To list all formatters run:

mouth list formatters

Contents:

feedr.formatters.fake_data(data_type)[source]

returns fake data for the data type requested.

will try and get fake data from the local format mappings. if fake data for the specific data type wasn’t found, it will try and use fake-factory to fake the data.

Parameters:data_type (string) – the type of data to fake.
Return type:string
class feedr.formatters.BaseFormatter(config)[source]

Bases: object

base class for all formatters

generate_data()[source]
f(config, name)[source]

retrieves configuration keys

if config wasn’t supplied or a key doesn’t exist, returns $RAND which will initiate data faking

class feedr.formatters.CustomFormatter(config)[source]

Bases: feedr.formatters.BaseFormatter

returns a generated log string in a custom format

this is also a formatter other formatters can rely on to generate application specific logs. see the ApacheAccessFormatter class for reference.

for every item in the format list, if an item in the data dict corresponds with it and the field’s data equals “$RAND”, use faker to fake an item for it. else, choose one item from the list randomly. if there no item in the data to correspond with the format, it will just append to format’s field name to the log.

example:

'CustomFormatter': {
    'format': ['name', ' - ', 'level'],
    'data': {
        'name': $RAND,
        'level': ['ERROR', 'DEBUG', 'INFO', 'CRITICAL'],
    }
}

the output of the above example might be:

Sally Fields - ERROR
or
Jason Banks - DEBUG
or
Danny Milwee - ERROR
or
...
generate_data()[source]

this will generate a message according to self.format with data from self.data.

all fields in the data dict will be iterated over and matched to the items in the format list. if a match is found and $RAND is set in one of the fields, random data will be generated for that field. If not, data will be chosen from the list. If no match is found, the explicit item in the format list will be appended.

example:

format = ['Mr. ' 'first_name', 'last_name']
data = {
    'first_name': ['Jason, Josh]',
    'last_name': '$RAND'
}

the output of the above example might be:

'Mr. Jason Williams'
or
'Mr. Josh Brolin'
or
'Mr. Jason Bananas'
...
class feedr.formatters.JsonFormatter(config)[source]

Bases: feedr.formatters.BaseFormatter

generates log strings in json format (or leave as dict)

all fields in the data dict will be iterated over. if $RAND is set in one of the fields, random data will be generated for that field. If not, data will be chosen from the list.

example:

'JsonFormatter': {
    'data': {
        'date_time': '$RAND',
        'level': ['ERROR', 'DEBUG'],
        'address': '$RAND',
    }
},

the output of the above example might be:

{"date_time": "2006-11-05 13:31:09", "name": "Miss Nona Breitenberg DVM", "level": "ERROR"}  # NOQA
or
{"date_time": "1985-01-20 11:41:16", "name": "Almeda Lindgren", "level": "DEBUG"}  # NOQA
or
{"date_time": "1973-05-21 01:06:04", "name": "Jase Heaney", "level": "DEBUG"}  # NOQA
or
...
generate_data()[source]
class feedr.formatters.ApacheAccessFormatter(config)[source]

Bases: feedr.formatters.CustomFormatter

returns an apache-access-log like string

you can easily construct new formatters by inheriting the custom formatter.

all you have to do is specify the format and the data. a helper method f is supplied in the BaseFormatter Class that will allow you to retrieve basic formatter configuration for your fields.

class feedr.formatters.ApacheAccessExFormatter(config)[source]

Bases: feedr.formatters.CustomFormatter

returns an apache-extended-access-log like string

class feedr.formatters.ApacheErrorFormatter(config)[source]

Bases: feedr.formatters.CustomFormatter

returns an apache-error-log like string