Skip to content

Instantly share code, notes, and snippets.

@rchrd2
Last active June 2, 2017 17:28
Show Gist options
  • Save rchrd2/722ccf16dff7f0030e5d971ea5486d61 to your computer and use it in GitHub Desktop.
Save rchrd2/722ccf16dff7f0030e5d971ea5486d61 to your computer and use it in GitHub Desktop.
newlog
#!/usr/bin/python3
"""
The idea is to take stdin, and write it to a new unique log file
php myscript.php | newlog filename
"""
import os
import sys
import datetime
import time
# Logs go to example /home/richard/logs
logdir = os.path.expanduser('~/logs')
# Set timezone for filename
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
def parse_stream():
now = datetime.datetime.now()
filename = now.strftime("%Y-%m-%d_%I_%M_%p")
if len(sys.argv) > 1:
filename = filename + '_' + sys.argv[1]
filename = logdir + '/' + filename + '.log'
with open(filename, 'w') as outfile:
for line in sys.stdin:
print(line, end="")
outfile.write(line)
print(filename)
if __name__ == '__main__':
parse_stream()
@rchrd2
Copy link
Author

rchrd2 commented Jun 2, 2017

On my desktop computer, I also use this to "stash" arbitrary notes.

I saved this script and named it stash, and changed the logdir to ~/Desktop/stash.

Example stashing a vim buffer:

$ vim
# write some stuff
:w !stash example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment