Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53b27ba

Browse files
Adding log
1 parent ab9096a commit 53b27ba

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
0 Bytes
Binary file not shown.

β€ŽREADME.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,86 @@ except:
793793

794794
```
795795

796+
<h3>Quick logger</h3>
797+
<p>Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through.</p>
798+
799+
```python
800+
801+
import os
802+
import time
803+
import subprocess
804+
import pathlib
805+
from pathlib import Path
806+
807+
currentVersion = "v1.0.5 - Release"
808+
pid = os.getpid()
809+
810+
LOGS_PATH = OUTPUT_PATH / Path("./logs")
811+
812+
def relative_to_logs(path: str) -> Path:
813+
"""Return a path relative to the logs folder."""
814+
return LOGS_PATH / Path(path)
815+
816+
def get_timestamp():
817+
"""Return a unix timestamp."""
818+
return time.time()
819+
820+
def logRoutine(log: str, timeNeeded: bool = True):
821+
"""Write strings to the log file and if debug is enabled, print it to console."""
822+
823+
if timeNeeded is None:
824+
timeNeeded = True
825+
826+
debugMode = False
827+
currentTime = time.strftime("%m-%d-%Y -> %H:%M:%S")
828+
logHeader = f"""{currentVersion}
829+
===================================================
830+
LOG FILE MADE FOR DEBUG PURPOSES
831+
made by Nicolas Mendes - September 2021
832+
===================================================\n
833+
"""
834+
835+
# Check if "ioc.log" exists, if not create this file.
836+
if not os.path.exists(relative_to_logs("ioc.log")):
837+
open(f"{relative_to_logs('ioc.log')}", "w+")
838+
# append logHeader to the file.
839+
with open(f"{relative_to_logs('ioc.log')}", "a") as logFile:
840+
logFile.write(logHeader)
841+
842+
# if the first line of ioc.log is different from currentVersion
843+
with open(f"{relative_to_logs('ioc.log')}") as checkVer:
844+
firstlineVer = checkVer.readline().rstrip()
845+
if firstlineVer != currentVersion:
846+
# Delete everything inside the file and append logHeader.
847+
with open(f"{relative_to_logs('ioc.log')}", "w+") as logFile:
848+
logFile.write(logHeader)
849+
850+
# if the file exceeds 1000 lines, delete everything and append logHeader to the file.
851+
with open(f"{relative_to_logs('ioc.log')}", "r") as logFile:
852+
if len(logFile.readlines()) > 1000:
853+
with open(f"{relative_to_logs('ioc.log')}", "w") as logFile:
854+
logFile.write(logHeader)
855+
856+
# Append the log to the file.
857+
858+
if timeNeeded == True:
859+
with open(f"{relative_to_logs('ioc.log')}", "a") as logFile:
860+
logFile.write(f"{currentTime} - {log}\n")
861+
else:
862+
with open(f"{relative_to_logs('ioc.log')}", "a") as logFile:
863+
logFile.write(f"{log}\n")
864+
865+
if debugMode == True:
866+
return print(f"DEBUG LOG: {log}")
867+
868+
logRoutine(
869+
f"\n\n[OK] ===> Python loaded. Starting new instance at PID: {pid} | UTS: {get_timestamp()}\n",
870+
False,
871+
)
872+
873+
```
874+
875+
796876
## πŸ“„ License
797877

798878
Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights.

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /