Skip to content

Instantly share code, notes, and snippets.

@jplsightm
Created October 14, 2020 19:10
Show Gist options
  • Save jplsightm/3ee58dd6a657d8642b61dcf95287dc6d to your computer and use it in GitHub Desktop.
Save jplsightm/3ee58dd6a657d8642b61dcf95287dc6d to your computer and use it in GitHub Desktop.
A very simple script that creates some silly logs in a json format. For demonstration purposes only.
import json
from datetime import datetime
from random import randint
from time import sleep
a = {'timestamp': datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), 'text': 'testing for the win, {}'.format(randint(0,10)), 'host': 'a_computer', 'attribute': 'abc'}
b = {'timestamp': datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), 'text': 'here_we_are for the win, {}'.format(randint(0,10)), 'host': 'a_computer', 'attribute': 'abc'}
c = {'timestamp': datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), 'text': 'testing for the win, {}'.format(randint(0,10)), 'host': 'b_computer', 'attribute': 'abc'}
d = {'timestamp': datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), 'text': '{} - what now'.format(randint(0,10)), 'host': 'b_computer', 'attribute': 'abc'}
e = {'timestamp': datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), 'text': 'testing for the win, {}'.format(randint(0,10)), 'host': 'c_computer', 'attribute': 'zyx'}
options = [a,b,c,d,e]
fname = 'test.log'
def main():
while True:
with open(fname, 'a') as fd:
ix = randint(0, 4)
try:
json.dump(options[ix], fd)
except Exception:
json.dump(options[0], fd)
fd.write('\n')
sleep(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment