[{"data":1,"prerenderedAt":1984},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-simple-logging-system-example":3},{"id":4,"title":5,"body":6,"description":1977,"extension":1978,"meta":1979,"navigation":62,"path":1980,"seo":1981,"stem":1982,"__hash__":1983},"content\u002Fexamples\u002Fpython-simple-logging-system-example.md","Python Simple Logging System Example",{"type":7,"value":8,"toc":1955},"minimark",[9,13,26,29,359,362,367,370,373,393,396,665,670,701,712,716,719,722,740,746,750,753,773,777,950,953,981,992,996,999,1023,1026,1252,1255,1259,1264,1267,1281,1284,1292,1295,1310,1313,1326,1329,1341,1344,1364,1368,1371,1374,1398,1401,1674,1686,1690,1693,1710,1714,1717,1788,1795,1802,1870,1877,1881,1885,1890,1894,1897,1901,1904,1908,1915,1919,1948,1951],[10,11,5],"h1",{"id":12},"python-simple-logging-system-example",[14,15,16,17,21,22,25],"p",{},"Build a small Python logging system that writes messages with timestamps and levels like ",[18,19,20],"code",{},"INFO"," and ",[18,23,24],{},"ERROR",". This example helps beginners understand how to record program activity in a simple, practical way.",[14,27,28],{},"If you want a quick working version first, use this:",[30,31,36],"pre",{"className":32,"code":33,"language":34,"meta":35,"style":35},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"ERROR\", \"Something went wrong\")\nprint(\"Messages written to app.log\")\n","python","",[18,37,38,57,64,69,116,159,208,213,266,283,288,293,318,342],{"__ignoreMap":35},[39,40,43,47,51,54],"span",{"class":41,"line":42},"line",1,[39,44,46],{"class":45},"sVHd0","from",[39,48,50],{"class":49},"su5hD"," datetime ",[39,52,53],{"class":45},"import",[39,55,56],{"class":49}," datetime\n",[39,58,60],{"class":41,"line":59},2,[39,61,63],{"emptyLinePlaceholder":62},true,"\n",[39,65,67],{"class":41,"line":66},3,[39,68,63],{"emptyLinePlaceholder":62},[39,70,72,76,80,84,88,91,94,96,99,103,107,111,113],{"class":41,"line":71},4,[39,73,75],{"class":74},"sbsja","def",[39,77,79],{"class":78},"sGLFI"," log_message",[39,81,83],{"class":82},"sP7_E","(",[39,85,87],{"class":86},"sFwrP","level",[39,89,90],{"class":82},",",[39,92,93],{"class":86}," message",[39,95,90],{"class":82},[39,97,98],{"class":86}," filename",[39,100,102],{"class":101},"smGrS","=",[39,104,106],{"class":105},"sjJ54","\"",[39,108,110],{"class":109},"s_sjI","app.log",[39,112,106],{"class":105},[39,114,115],{"class":82},"):\n",[39,117,119,122,124,127,130,134,137,140,142,144,147,151,154,156],{"class":41,"line":118},5,[39,120,121],{"class":49},"    timestamp ",[39,123,102],{"class":101},[39,125,126],{"class":49}," datetime",[39,128,129],{"class":82},".",[39,131,133],{"class":132},"slqww","now",[39,135,136],{"class":82},"().",[39,138,139],{"class":132},"strftime",[39,141,83],{"class":82},[39,143,106],{"class":105},[39,145,146],{"class":109},"%Y-%m-",[39,148,150],{"class":149},"srdBf","%d",[39,152,153],{"class":109}," %H:%M:%S",[39,155,106],{"class":105},[39,157,158],{"class":82},")\n",[39,160,162,165,167,170,173,176,179,182,185,187,189,191,194,196,199,201,205],{"class":41,"line":161},6,[39,163,164],{"class":49},"    line ",[39,166,102],{"class":101},[39,168,169],{"class":74}," f",[39,171,172],{"class":109},"\"[",[39,174,175],{"class":149},"{",[39,177,178],{"class":49},"timestamp",[39,180,181],{"class":149},"}",[39,183,184],{"class":109},"] ",[39,186,175],{"class":149},[39,188,87],{"class":49},[39,190,181],{"class":149},[39,192,193],{"class":109},": ",[39,195,175],{"class":149},[39,197,198],{"class":49},"message",[39,200,181],{"class":149},[39,202,204],{"class":203},"s_hVV","\\n",[39,206,207],{"class":109},"\"\n",[39,209,211],{"class":41,"line":210},7,[39,212,63],{"emptyLinePlaceholder":62},[39,214,216,219,223,225,228,230,233,236,238,240,244,246,248,251,253,256,259,263],{"class":41,"line":215},8,[39,217,218],{"class":45},"    with",[39,220,222],{"class":221},"sptTA"," open",[39,224,83],{"class":82},[39,226,227],{"class":132},"filename",[39,229,90],{"class":82},[39,231,232],{"class":105}," \"",[39,234,235],{"class":109},"a",[39,237,106],{"class":105},[39,239,90],{"class":82},[39,241,243],{"class":242},"s99_P"," encoding",[39,245,102],{"class":101},[39,247,106],{"class":105},[39,249,250],{"class":109},"utf-8",[39,252,106],{"class":105},[39,254,255],{"class":82},")",[39,257,258],{"class":45}," as",[39,260,262],{"class":261},"sMMDD"," file",[39,264,265],{"class":82},":\n",[39,267,269,272,274,277,279,281],{"class":41,"line":268},9,[39,270,271],{"class":261},"        file",[39,273,129],{"class":82},[39,275,276],{"class":132},"write",[39,278,83],{"class":82},[39,280,41],{"class":132},[39,282,158],{"class":82},[39,284,286],{"class":41,"line":285},10,[39,287,63],{"emptyLinePlaceholder":62},[39,289,291],{"class":41,"line":290},11,[39,292,63],{"emptyLinePlaceholder":62},[39,294,296,299,301,303,305,307,309,311,314,316],{"class":41,"line":295},12,[39,297,298],{"class":132},"log_message",[39,300,83],{"class":82},[39,302,106],{"class":105},[39,304,20],{"class":109},[39,306,106],{"class":105},[39,308,90],{"class":82},[39,310,232],{"class":105},[39,312,313],{"class":109},"Program started",[39,315,106],{"class":105},[39,317,158],{"class":82},[39,319,321,323,325,327,329,331,333,335,338,340],{"class":41,"line":320},13,[39,322,298],{"class":132},[39,324,83],{"class":82},[39,326,106],{"class":105},[39,328,24],{"class":109},[39,330,106],{"class":105},[39,332,90],{"class":82},[39,334,232],{"class":105},[39,336,337],{"class":109},"Something went wrong",[39,339,106],{"class":105},[39,341,158],{"class":82},[39,343,345,348,350,352,355,357],{"class":41,"line":344},14,[39,346,347],{"class":221},"print",[39,349,83],{"class":82},[39,351,106],{"class":105},[39,353,354],{"class":109},"Messages written to app.log",[39,356,106],{"class":105},[39,358,158],{"class":82},[14,360,361],{},"This version appends log messages to a file so old messages are not erased.",[363,364,366],"h2",{"id":365},"what-this-example-does","What this example does",[14,368,369],{},"This script creates a very simple logging system.",[14,371,372],{},"It:",[374,375,376,380,383,386],"ul",{},[377,378,379],"li",{},"Creates a reusable function for writing log messages",[377,381,382],{},"Adds a timestamp to each message",[377,384,385],{},"Stores logs in a text file",[377,387,388,389,21,391],{},"Uses simple log levels such as ",[18,390,20],{},[18,392,24],{},[14,394,395],{},"Here is the full example again:",[30,397,399],{"className":32,"code":398,"language":34,"meta":35,"style":35},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"INFO\", \"Loading user data\")\nlog_message(\"ERROR\", \"Could not connect to server\")\n\nprint(\"Messages written to app.log\")\n",[18,400,401,411,415,419,447,477,513,517,555,569,573,577,599,622,645,650],{"__ignoreMap":35},[39,402,403,405,407,409],{"class":41,"line":42},[39,404,46],{"class":45},[39,406,50],{"class":49},[39,408,53],{"class":45},[39,410,56],{"class":49},[39,412,413],{"class":41,"line":59},[39,414,63],{"emptyLinePlaceholder":62},[39,416,417],{"class":41,"line":66},[39,418,63],{"emptyLinePlaceholder":62},[39,420,421,423,425,427,429,431,433,435,437,439,441,443,445],{"class":41,"line":71},[39,422,75],{"class":74},[39,424,79],{"class":78},[39,426,83],{"class":82},[39,428,87],{"class":86},[39,430,90],{"class":82},[39,432,93],{"class":86},[39,434,90],{"class":82},[39,436,98],{"class":86},[39,438,102],{"class":101},[39,440,106],{"class":105},[39,442,110],{"class":109},[39,444,106],{"class":105},[39,446,115],{"class":82},[39,448,449,451,453,455,457,459,461,463,465,467,469,471,473,475],{"class":41,"line":118},[39,450,121],{"class":49},[39,452,102],{"class":101},[39,454,126],{"class":49},[39,456,129],{"class":82},[39,458,133],{"class":132},[39,460,136],{"class":82},[39,462,139],{"class":132},[39,464,83],{"class":82},[39,466,106],{"class":105},[39,468,146],{"class":109},[39,470,150],{"class":149},[39,472,153],{"class":109},[39,474,106],{"class":105},[39,476,158],{"class":82},[39,478,479,481,483,485,487,489,491,493,495,497,499,501,503,505,507,509,511],{"class":41,"line":161},[39,480,164],{"class":49},[39,482,102],{"class":101},[39,484,169],{"class":74},[39,486,172],{"class":109},[39,488,175],{"class":149},[39,490,178],{"class":49},[39,492,181],{"class":149},[39,494,184],{"class":109},[39,496,175],{"class":149},[39,498,87],{"class":49},[39,500,181],{"class":149},[39,502,193],{"class":109},[39,504,175],{"class":149},[39,506,198],{"class":49},[39,508,181],{"class":149},[39,510,204],{"class":203},[39,512,207],{"class":109},[39,514,515],{"class":41,"line":210},[39,516,63],{"emptyLinePlaceholder":62},[39,518,519,521,523,525,527,529,531,533,535,537,539,541,543,545,547,549,551,553],{"class":41,"line":215},[39,520,218],{"class":45},[39,522,222],{"class":221},[39,524,83],{"class":82},[39,526,227],{"class":132},[39,528,90],{"class":82},[39,530,232],{"class":105},[39,532,235],{"class":109},[39,534,106],{"class":105},[39,536,90],{"class":82},[39,538,243],{"class":242},[39,540,102],{"class":101},[39,542,106],{"class":105},[39,544,250],{"class":109},[39,546,106],{"class":105},[39,548,255],{"class":82},[39,550,258],{"class":45},[39,552,262],{"class":261},[39,554,265],{"class":82},[39,556,557,559,561,563,565,567],{"class":41,"line":268},[39,558,271],{"class":261},[39,560,129],{"class":82},[39,562,276],{"class":132},[39,564,83],{"class":82},[39,566,41],{"class":132},[39,568,158],{"class":82},[39,570,571],{"class":41,"line":285},[39,572,63],{"emptyLinePlaceholder":62},[39,574,575],{"class":41,"line":290},[39,576,63],{"emptyLinePlaceholder":62},[39,578,579,581,583,585,587,589,591,593,595,597],{"class":41,"line":295},[39,580,298],{"class":132},[39,582,83],{"class":82},[39,584,106],{"class":105},[39,586,20],{"class":109},[39,588,106],{"class":105},[39,590,90],{"class":82},[39,592,232],{"class":105},[39,594,313],{"class":109},[39,596,106],{"class":105},[39,598,158],{"class":82},[39,600,601,603,605,607,609,611,613,615,618,620],{"class":41,"line":320},[39,602,298],{"class":132},[39,604,83],{"class":82},[39,606,106],{"class":105},[39,608,20],{"class":109},[39,610,106],{"class":105},[39,612,90],{"class":82},[39,614,232],{"class":105},[39,616,617],{"class":109},"Loading user data",[39,619,106],{"class":105},[39,621,158],{"class":82},[39,623,624,626,628,630,632,634,636,638,641,643],{"class":41,"line":344},[39,625,298],{"class":132},[39,627,83],{"class":82},[39,629,106],{"class":105},[39,631,24],{"class":109},[39,633,106],{"class":105},[39,635,90],{"class":82},[39,637,232],{"class":105},[39,639,640],{"class":109},"Could not connect to server",[39,642,106],{"class":105},[39,644,158],{"class":82},[39,646,648],{"class":41,"line":647},15,[39,649,63],{"emptyLinePlaceholder":62},[39,651,653,655,657,659,661,663],{"class":41,"line":652},16,[39,654,347],{"class":221},[39,656,83],{"class":82},[39,658,106],{"class":105},[39,660,354],{"class":109},[39,662,106],{"class":105},[39,664,158],{"class":82},[666,667,669],"h3",{"id":668},"how-it-works","How it works",[374,671,672,678,684,689,695],{},[377,673,674,677],{},[18,675,676],{},"datetime.now()"," gets the current date and time",[377,679,680,683],{},[18,681,682],{},"strftime(\"%Y-%m-%d %H:%M:%S\")"," turns that date and time into readable text",[377,685,686,687],{},"The formatted log line is stored in ",[18,688,41],{},[377,690,691,694],{},[18,692,693],{},"open(filename, \"a\", encoding=\"utf-8\")"," opens the file in append mode",[377,696,697,700],{},[18,698,699],{},"file.write(line)"," adds the message to the file",[14,702,703,704,21,708,129],{},"If you are new to writing files, see ",[235,705,707],{"href":706},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python\u002F","how to write to a file in Python",[235,709,711],{"href":710},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","how to append to a file in Python",[363,713,715],{"id":714},"why-logging-is-useful","Why logging is useful",[14,717,718],{},"Logging helps you keep a record of what your program did.",[14,720,721],{},"This is useful because it:",[374,723,724,727,730,733],{},[377,725,726],{},"Helps you see what your program did",[377,728,729],{},"Makes debugging easier",[377,731,732],{},"Lets you keep a record of errors",[377,734,735,736,739],{},"Is more useful than ",[18,737,738],{},"print()"," when you want saved output",[14,741,742,743,745],{},"For example, ",[18,744,738],{}," only shows text in the terminal while the program is running. A log file stays on your computer, so you can check it later.",[363,747,749],{"id":748},"core-parts-of-the-script","Core parts of the script",[14,751,752],{},"These are the most important parts of the example:",[374,754,755,759,765,770],{},[377,756,757,677],{},[18,758,676],{},[377,760,761,764],{},[18,762,763],{},"strftime()"," formats the timestamp as readable text",[377,766,767,694],{},[18,768,769],{},"open(filename, \"a\")",[377,771,772],{},"The log function keeps the code organized and reusable",[666,774,776],{"id":775},"the-logging-function","The logging function",[30,778,780],{"className":32,"code":779,"language":34,"meta":35,"style":35},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n",[18,781,782,792,796,800,828,858,894,898,936],{"__ignoreMap":35},[39,783,784,786,788,790],{"class":41,"line":42},[39,785,46],{"class":45},[39,787,50],{"class":49},[39,789,53],{"class":45},[39,791,56],{"class":49},[39,793,794],{"class":41,"line":59},[39,795,63],{"emptyLinePlaceholder":62},[39,797,798],{"class":41,"line":66},[39,799,63],{"emptyLinePlaceholder":62},[39,801,802,804,806,808,810,812,814,816,818,820,822,824,826],{"class":41,"line":71},[39,803,75],{"class":74},[39,805,79],{"class":78},[39,807,83],{"class":82},[39,809,87],{"class":86},[39,811,90],{"class":82},[39,813,93],{"class":86},[39,815,90],{"class":82},[39,817,98],{"class":86},[39,819,102],{"class":101},[39,821,106],{"class":105},[39,823,110],{"class":109},[39,825,106],{"class":105},[39,827,115],{"class":82},[39,829,830,832,834,836,838,840,842,844,846,848,850,852,854,856],{"class":41,"line":118},[39,831,121],{"class":49},[39,833,102],{"class":101},[39,835,126],{"class":49},[39,837,129],{"class":82},[39,839,133],{"class":132},[39,841,136],{"class":82},[39,843,139],{"class":132},[39,845,83],{"class":82},[39,847,106],{"class":105},[39,849,146],{"class":109},[39,851,150],{"class":149},[39,853,153],{"class":109},[39,855,106],{"class":105},[39,857,158],{"class":82},[39,859,860,862,864,866,868,870,872,874,876,878,880,882,884,886,888,890,892],{"class":41,"line":161},[39,861,164],{"class":49},[39,863,102],{"class":101},[39,865,169],{"class":74},[39,867,172],{"class":109},[39,869,175],{"class":149},[39,871,178],{"class":49},[39,873,181],{"class":149},[39,875,184],{"class":109},[39,877,175],{"class":149},[39,879,87],{"class":49},[39,881,181],{"class":149},[39,883,193],{"class":109},[39,885,175],{"class":149},[39,887,198],{"class":49},[39,889,181],{"class":149},[39,891,204],{"class":203},[39,893,207],{"class":109},[39,895,896],{"class":41,"line":210},[39,897,63],{"emptyLinePlaceholder":62},[39,899,900,902,904,906,908,910,912,914,916,918,920,922,924,926,928,930,932,934],{"class":41,"line":215},[39,901,218],{"class":45},[39,903,222],{"class":221},[39,905,83],{"class":82},[39,907,227],{"class":132},[39,909,90],{"class":82},[39,911,232],{"class":105},[39,913,235],{"class":109},[39,915,106],{"class":105},[39,917,90],{"class":82},[39,919,243],{"class":242},[39,921,102],{"class":101},[39,923,106],{"class":105},[39,925,250],{"class":109},[39,927,106],{"class":105},[39,929,255],{"class":82},[39,931,258],{"class":45},[39,933,262],{"class":261},[39,935,265],{"class":82},[39,937,938,940,942,944,946,948],{"class":41,"line":268},[39,939,271],{"class":261},[39,941,129],{"class":82},[39,943,276],{"class":132},[39,945,83],{"class":82},[39,947,41],{"class":132},[39,949,158],{"class":82},[14,951,952],{},"Key lines:",[374,954,955,965,970,976],{},[377,956,957,959,960,962,963],{},[18,958,87],{}," is a label such as ",[18,961,20],{}," or ",[18,964,24],{},[377,966,967,969],{},[18,968,198],{}," is the text you want to save",[377,971,972,975],{},[18,973,974],{},"filename=\"app.log\""," gives the function a default log file name",[377,977,978,980],{},[18,979,204],{}," adds a newline so each log entry appears on its own line",[14,982,983,984,987,988,129],{},"If you want to understand ",[18,985,986],{},"open()"," better, see ",[235,989,991],{"href":990},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[363,993,995],{"id":994},"step-by-step-build-order","Step-by-step build order",[14,997,998],{},"A good way to build this script is:",[1000,1001,1002,1008,1011,1014,1017,1020],"ol",{},[377,1003,1004,1005],{},"Import ",[18,1006,1007],{},"datetime",[377,1009,1010],{},"Create a function that accepts a level and message",[377,1012,1013],{},"Build the log line as a string",[377,1015,1016],{},"Open the log file in append mode",[377,1018,1019],{},"Write the line to the file",[377,1021,1022],{},"Call the function with different message types",[14,1024,1025],{},"Here is the same process as code:",[30,1027,1029],{"className":32,"code":1028,"language":34,"meta":35,"style":35},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"ERROR\", \"File not found\")\n",[18,1030,1031,1041,1045,1049,1077,1107,1143,1147,1185,1199,1203,1207,1229],{"__ignoreMap":35},[39,1032,1033,1035,1037,1039],{"class":41,"line":42},[39,1034,46],{"class":45},[39,1036,50],{"class":49},[39,1038,53],{"class":45},[39,1040,56],{"class":49},[39,1042,1043],{"class":41,"line":59},[39,1044,63],{"emptyLinePlaceholder":62},[39,1046,1047],{"class":41,"line":66},[39,1048,63],{"emptyLinePlaceholder":62},[39,1050,1051,1053,1055,1057,1059,1061,1063,1065,1067,1069,1071,1073,1075],{"class":41,"line":71},[39,1052,75],{"class":74},[39,1054,79],{"class":78},[39,1056,83],{"class":82},[39,1058,87],{"class":86},[39,1060,90],{"class":82},[39,1062,93],{"class":86},[39,1064,90],{"class":82},[39,1066,98],{"class":86},[39,1068,102],{"class":101},[39,1070,106],{"class":105},[39,1072,110],{"class":109},[39,1074,106],{"class":105},[39,1076,115],{"class":82},[39,1078,1079,1081,1083,1085,1087,1089,1091,1093,1095,1097,1099,1101,1103,1105],{"class":41,"line":118},[39,1080,121],{"class":49},[39,1082,102],{"class":101},[39,1084,126],{"class":49},[39,1086,129],{"class":82},[39,1088,133],{"class":132},[39,1090,136],{"class":82},[39,1092,139],{"class":132},[39,1094,83],{"class":82},[39,1096,106],{"class":105},[39,1098,146],{"class":109},[39,1100,150],{"class":149},[39,1102,153],{"class":109},[39,1104,106],{"class":105},[39,1106,158],{"class":82},[39,1108,1109,1111,1113,1115,1117,1119,1121,1123,1125,1127,1129,1131,1133,1135,1137,1139,1141],{"class":41,"line":161},[39,1110,164],{"class":49},[39,1112,102],{"class":101},[39,1114,169],{"class":74},[39,1116,172],{"class":109},[39,1118,175],{"class":149},[39,1120,178],{"class":49},[39,1122,181],{"class":149},[39,1124,184],{"class":109},[39,1126,175],{"class":149},[39,1128,87],{"class":49},[39,1130,181],{"class":149},[39,1132,193],{"class":109},[39,1134,175],{"class":149},[39,1136,198],{"class":49},[39,1138,181],{"class":149},[39,1140,204],{"class":203},[39,1142,207],{"class":109},[39,1144,1145],{"class":41,"line":210},[39,1146,63],{"emptyLinePlaceholder":62},[39,1148,1149,1151,1153,1155,1157,1159,1161,1163,1165,1167,1169,1171,1173,1175,1177,1179,1181,1183],{"class":41,"line":215},[39,1150,218],{"class":45},[39,1152,222],{"class":221},[39,1154,83],{"class":82},[39,1156,227],{"class":132},[39,1158,90],{"class":82},[39,1160,232],{"class":105},[39,1162,235],{"class":109},[39,1164,106],{"class":105},[39,1166,90],{"class":82},[39,1168,243],{"class":242},[39,1170,102],{"class":101},[39,1172,106],{"class":105},[39,1174,250],{"class":109},[39,1176,106],{"class":105},[39,1178,255],{"class":82},[39,1180,258],{"class":45},[39,1182,262],{"class":261},[39,1184,265],{"class":82},[39,1186,1187,1189,1191,1193,1195,1197],{"class":41,"line":268},[39,1188,271],{"class":261},[39,1190,129],{"class":82},[39,1192,276],{"class":132},[39,1194,83],{"class":82},[39,1196,41],{"class":132},[39,1198,158],{"class":82},[39,1200,1201],{"class":41,"line":285},[39,1202,63],{"emptyLinePlaceholder":62},[39,1204,1205],{"class":41,"line":290},[39,1206,63],{"emptyLinePlaceholder":62},[39,1208,1209,1211,1213,1215,1217,1219,1221,1223,1225,1227],{"class":41,"line":295},[39,1210,298],{"class":132},[39,1212,83],{"class":82},[39,1214,106],{"class":105},[39,1216,20],{"class":109},[39,1218,106],{"class":105},[39,1220,90],{"class":82},[39,1222,232],{"class":105},[39,1224,313],{"class":109},[39,1226,106],{"class":105},[39,1228,158],{"class":82},[39,1230,1231,1233,1235,1237,1239,1241,1243,1245,1248,1250],{"class":41,"line":320},[39,1232,298],{"class":132},[39,1234,83],{"class":82},[39,1236,106],{"class":105},[39,1238,24],{"class":109},[39,1240,106],{"class":105},[39,1242,90],{"class":82},[39,1244,232],{"class":105},[39,1246,1247],{"class":109},"File not found",[39,1249,106],{"class":105},[39,1251,158],{"class":82},[14,1253,1254],{},"This keeps the script simple and easy to reuse in other small programs.",[363,1256,1258],{"id":1257},"expected-output-in-the-log-file","Expected output in the log file",[14,1260,1261,1262,129],{},"Lines are saved in ",[18,1263,110],{},[14,1265,1266],{},"Each line includes:",[374,1268,1269,1272,1275,1278],{},[377,1270,1271],{},"Date",[377,1273,1274],{},"Time",[377,1276,1277],{},"Level",[377,1279,1280],{},"Message",[14,1282,1283],{},"Example:",[30,1285,1290],{"className":1286,"code":1288,"language":1289,"meta":35},[1287],"language-text","[2026-04-22 10:30:15] INFO: Program started\n[2026-04-22 10:30:16] ERROR: Something went wrong\n","text",[18,1291,1288],{"__ignoreMap":35},[14,1293,1294],{},"To run the script:",[30,1296,1300],{"className":1297,"code":1298,"language":1299,"meta":35,"style":35},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python script.py\n","bash",[18,1301,1302],{"__ignoreMap":35},[39,1303,1304,1307],{"class":41,"line":42},[39,1305,34],{"class":1306},"sbgvK",[39,1308,1309],{"class":109}," script.py\n",[14,1311,1312],{},"To check the log file on macOS or Linux:",[30,1314,1316],{"className":1297,"code":1315,"language":1299,"meta":35,"style":35},"cat app.log\n",[18,1317,1318],{"__ignoreMap":35},[39,1319,1320,1323],{"class":41,"line":42},[39,1321,1322],{"class":1306},"cat",[39,1324,1325],{"class":109}," app.log\n",[14,1327,1328],{},"To check the log file on Windows Command Prompt:",[30,1330,1332],{"className":1297,"code":1331,"language":1299,"meta":35,"style":35},"type app.log\n",[18,1333,1334],{"__ignoreMap":35},[39,1335,1336,1339],{"class":41,"line":42},[39,1337,1338],{"class":221},"type",[39,1340,1325],{"class":109},[14,1342,1343],{},"To see the full path of the log file:",[30,1345,1347],{"className":1297,"code":1346,"language":1299,"meta":35,"style":35},"python -c \"from pathlib import Path; print(Path('app.log').resolve())\"\n",[18,1348,1349],{"__ignoreMap":35},[39,1350,1351,1353,1357,1359,1362],{"class":41,"line":42},[39,1352,34],{"class":1306},[39,1354,1356],{"class":1355},"stzsN"," -c",[39,1358,232],{"class":105},[39,1360,1361],{"class":109},"from pathlib import Path; print(Path('app.log').resolve())",[39,1363,207],{"class":105},[363,1365,1367],{"id":1366},"easy-ways-to-improve-this-example","Easy ways to improve this example",[14,1369,1370],{},"Once the basic version works, you can extend it.",[14,1372,1373],{},"Easy improvements include:",[374,1375,1376,1385,1388,1391],{},[377,1377,1378,1379,962,1382],{},"Add more log levels such as ",[18,1380,1381],{},"WARNING",[18,1383,1384],{},"DEBUG",[377,1386,1387],{},"Print the message to the screen and save it to the file",[377,1389,1390],{},"Use a different file name for each day",[377,1392,1393,1394,1397],{},"Wrap risky code in ",[18,1395,1396],{},"try-except"," and log errors",[14,1399,1400],{},"Here is a slightly improved version that prints and logs at the same time:",[30,1402,1404],{"className":32,"code":1403,"language":34,"meta":35,"style":35},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\"\n\n    print(line)\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line + \"\\n\")\n\n\nlog_message(\"INFO\", \"User opened the program\")\nlog_message(\"WARNING\", \"Input was empty\")\nlog_message(\"ERROR\", \"Could not save file\")\n",[18,1405,1406,1416,1420,1424,1452,1482,1516,1520,1531,1535,1573,1597,1601,1605,1628,1651],{"__ignoreMap":35},[39,1407,1408,1410,1412,1414],{"class":41,"line":42},[39,1409,46],{"class":45},[39,1411,50],{"class":49},[39,1413,53],{"class":45},[39,1415,56],{"class":49},[39,1417,1418],{"class":41,"line":59},[39,1419,63],{"emptyLinePlaceholder":62},[39,1421,1422],{"class":41,"line":66},[39,1423,63],{"emptyLinePlaceholder":62},[39,1425,1426,1428,1430,1432,1434,1436,1438,1440,1442,1444,1446,1448,1450],{"class":41,"line":71},[39,1427,75],{"class":74},[39,1429,79],{"class":78},[39,1431,83],{"class":82},[39,1433,87],{"class":86},[39,1435,90],{"class":82},[39,1437,93],{"class":86},[39,1439,90],{"class":82},[39,1441,98],{"class":86},[39,1443,102],{"class":101},[39,1445,106],{"class":105},[39,1447,110],{"class":109},[39,1449,106],{"class":105},[39,1451,115],{"class":82},[39,1453,1454,1456,1458,1460,1462,1464,1466,1468,1470,1472,1474,1476,1478,1480],{"class":41,"line":118},[39,1455,121],{"class":49},[39,1457,102],{"class":101},[39,1459,126],{"class":49},[39,1461,129],{"class":82},[39,1463,133],{"class":132},[39,1465,136],{"class":82},[39,1467,139],{"class":132},[39,1469,83],{"class":82},[39,1471,106],{"class":105},[39,1473,146],{"class":109},[39,1475,150],{"class":149},[39,1477,153],{"class":109},[39,1479,106],{"class":105},[39,1481,158],{"class":82},[39,1483,1484,1486,1488,1490,1492,1494,1496,1498,1500,1502,1504,1506,1508,1510,1512,1514],{"class":41,"line":161},[39,1485,164],{"class":49},[39,1487,102],{"class":101},[39,1489,169],{"class":74},[39,1491,172],{"class":109},[39,1493,175],{"class":149},[39,1495,178],{"class":49},[39,1497,181],{"class":149},[39,1499,184],{"class":109},[39,1501,175],{"class":149},[39,1503,87],{"class":49},[39,1505,181],{"class":149},[39,1507,193],{"class":109},[39,1509,175],{"class":149},[39,1511,198],{"class":49},[39,1513,181],{"class":149},[39,1515,207],{"class":109},[39,1517,1518],{"class":41,"line":210},[39,1519,63],{"emptyLinePlaceholder":62},[39,1521,1522,1525,1527,1529],{"class":41,"line":215},[39,1523,1524],{"class":221},"    print",[39,1526,83],{"class":82},[39,1528,41],{"class":132},[39,1530,158],{"class":82},[39,1532,1533],{"class":41,"line":268},[39,1534,63],{"emptyLinePlaceholder":62},[39,1536,1537,1539,1541,1543,1545,1547,1549,1551,1553,1555,1557,1559,1561,1563,1565,1567,1569,1571],{"class":41,"line":285},[39,1538,218],{"class":45},[39,1540,222],{"class":221},[39,1542,83],{"class":82},[39,1544,227],{"class":132},[39,1546,90],{"class":82},[39,1548,232],{"class":105},[39,1550,235],{"class":109},[39,1552,106],{"class":105},[39,1554,90],{"class":82},[39,1556,243],{"class":242},[39,1558,102],{"class":101},[39,1560,106],{"class":105},[39,1562,250],{"class":109},[39,1564,106],{"class":105},[39,1566,255],{"class":82},[39,1568,258],{"class":45},[39,1570,262],{"class":261},[39,1572,265],{"class":82},[39,1574,1575,1577,1579,1581,1583,1586,1589,1591,1593,1595],{"class":41,"line":290},[39,1576,271],{"class":261},[39,1578,129],{"class":82},[39,1580,276],{"class":132},[39,1582,83],{"class":82},[39,1584,1585],{"class":132},"line ",[39,1587,1588],{"class":101},"+",[39,1590,232],{"class":105},[39,1592,204],{"class":203},[39,1594,106],{"class":105},[39,1596,158],{"class":82},[39,1598,1599],{"class":41,"line":295},[39,1600,63],{"emptyLinePlaceholder":62},[39,1602,1603],{"class":41,"line":320},[39,1604,63],{"emptyLinePlaceholder":62},[39,1606,1607,1609,1611,1613,1615,1617,1619,1621,1624,1626],{"class":41,"line":344},[39,1608,298],{"class":132},[39,1610,83],{"class":82},[39,1612,106],{"class":105},[39,1614,20],{"class":109},[39,1616,106],{"class":105},[39,1618,90],{"class":82},[39,1620,232],{"class":105},[39,1622,1623],{"class":109},"User opened the program",[39,1625,106],{"class":105},[39,1627,158],{"class":82},[39,1629,1630,1632,1634,1636,1638,1640,1642,1644,1647,1649],{"class":41,"line":647},[39,1631,298],{"class":132},[39,1633,83],{"class":82},[39,1635,106],{"class":105},[39,1637,1381],{"class":109},[39,1639,106],{"class":105},[39,1641,90],{"class":82},[39,1643,232],{"class":105},[39,1645,1646],{"class":109},"Input was empty",[39,1648,106],{"class":105},[39,1650,158],{"class":82},[39,1652,1653,1655,1657,1659,1661,1663,1665,1667,1670,1672],{"class":41,"line":652},[39,1654,298],{"class":132},[39,1656,83],{"class":82},[39,1658,106],{"class":105},[39,1660,24],{"class":109},[39,1662,106],{"class":105},[39,1664,90],{"class":82},[39,1666,232],{"class":105},[39,1668,1669],{"class":109},"Could not save file",[39,1671,106],{"class":105},[39,1673,158],{"class":82},[14,1675,1676,1677,1681,1682,129],{},"If you want to handle errors while logging program problems, see ",[235,1678,1680],{"href":1679},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python"," and the ",[235,1683,1685],{"href":1684},"\u002Fexamples\u002Fpython-error-handling-example-script\u002F","Python error handling example script",[363,1687,1689],{"id":1688},"common-mistakes","Common mistakes",[14,1691,1692],{},"These are common reasons beginners think the script is not working:",[374,1694,1695,1698,1701,1704,1707],{},[377,1696,1697],{},"Opening the file in write mode instead of append mode, which erases old logs",[377,1699,1700],{},"Forgetting the newline character so all logs appear on one line",[377,1702,1703],{},"Using a file path that does not exist",[377,1705,1706],{},"Misspelling the filename and checking the wrong file",[377,1708,1709],{},"Expecting logging to work without calling the function",[666,1711,1713],{"id":1712},"example-of-a-common-mistake","Example of a common mistake",[14,1715,1716],{},"This version uses write mode:",[30,1718,1720],{"className":32,"code":1719,"language":34,"meta":35,"style":35},"with open(\"app.log\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"This will replace the old file contents.\\n\")\n",[18,1721,1722,1766],{"__ignoreMap":35},[39,1723,1724,1727,1729,1731,1733,1735,1737,1739,1741,1744,1746,1748,1750,1752,1754,1756,1758,1760,1762,1764],{"class":41,"line":42},[39,1725,1726],{"class":45},"with",[39,1728,222],{"class":221},[39,1730,83],{"class":82},[39,1732,106],{"class":105},[39,1734,110],{"class":109},[39,1736,106],{"class":105},[39,1738,90],{"class":82},[39,1740,232],{"class":105},[39,1742,1743],{"class":109},"w",[39,1745,106],{"class":105},[39,1747,90],{"class":82},[39,1749,243],{"class":242},[39,1751,102],{"class":101},[39,1753,106],{"class":105},[39,1755,250],{"class":109},[39,1757,106],{"class":105},[39,1759,255],{"class":82},[39,1761,258],{"class":45},[39,1763,262],{"class":261},[39,1765,265],{"class":82},[39,1767,1768,1771,1773,1775,1777,1779,1782,1784,1786],{"class":41,"line":59},[39,1769,1770],{"class":261},"    file",[39,1772,129],{"class":82},[39,1774,276],{"class":132},[39,1776,83],{"class":82},[39,1778,106],{"class":105},[39,1780,1781],{"class":109},"This will replace the old file contents.",[39,1783,204],{"class":203},[39,1785,106],{"class":105},[39,1787,158],{"class":82},[14,1789,1790,1791,1794],{},"Mode ",[18,1792,1793],{},"\"w\""," creates a new file or overwrites an existing file.",[14,1796,1797,1798,1801],{},"If you want to keep old log entries, use ",[18,1799,1800],{},"\"a\""," instead:",[30,1803,1805],{"className":32,"code":1804,"language":34,"meta":35,"style":35},"with open(\"app.log\", \"a\", encoding=\"utf-8\") as file:\n    file.write(\"This will be added to the end of the file.\\n\")\n",[18,1806,1807,1849],{"__ignoreMap":35},[39,1808,1809,1811,1813,1815,1817,1819,1821,1823,1825,1827,1829,1831,1833,1835,1837,1839,1841,1843,1845,1847],{"class":41,"line":42},[39,1810,1726],{"class":45},[39,1812,222],{"class":221},[39,1814,83],{"class":82},[39,1816,106],{"class":105},[39,1818,110],{"class":109},[39,1820,106],{"class":105},[39,1822,90],{"class":82},[39,1824,232],{"class":105},[39,1826,235],{"class":109},[39,1828,106],{"class":105},[39,1830,90],{"class":82},[39,1832,243],{"class":242},[39,1834,102],{"class":101},[39,1836,106],{"class":105},[39,1838,250],{"class":109},[39,1840,106],{"class":105},[39,1842,255],{"class":82},[39,1844,258],{"class":45},[39,1846,262],{"class":261},[39,1848,265],{"class":82},[39,1850,1851,1853,1855,1857,1859,1861,1864,1866,1868],{"class":41,"line":59},[39,1852,1770],{"class":261},[39,1854,129],{"class":82},[39,1856,276],{"class":132},[39,1858,83],{"class":82},[39,1860,106],{"class":105},[39,1862,1863],{"class":109},"This will be added to the end of the file.",[39,1865,204],{"class":203},[39,1867,106],{"class":105},[39,1869,158],{"class":82},[14,1871,1872,1873,129],{},"For a broader introduction to files, see ",[235,1874,1876],{"href":1875},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics: read and write",[363,1878,1880],{"id":1879},"faq","FAQ",[666,1882,1884],{"id":1883},"what-is-the-difference-between-logging-and-print","What is the difference between logging and print()?",[14,1886,1887,1889],{},[18,1888,738],{}," shows output on the screen. Logging saves messages so you can review them later.",[666,1891,1893],{"id":1892},"why-use-append-mode-when-opening-the-file","Why use append mode when opening the file?",[14,1895,1896],{},"Append mode adds new messages to the end of the file instead of deleting old ones.",[666,1898,1900],{"id":1899},"can-i-log-errors-only","Can I log errors only?",[14,1902,1903],{},"Yes. You can call the function only when an error happens, but logging normal steps can also help with debugging.",[666,1905,1907],{"id":1906},"do-i-need-the-logging-module-for-this-example","Do I need the logging module for this example?",[14,1909,1910,1911,1914],{},"No. This page shows a simple beginner-friendly version. The standard ",[18,1912,1913],{},"logging"," module is more powerful but more advanced.",[363,1916,1918],{"id":1917},"see-also","See also",[374,1920,1921,1926,1931,1935,1939,1944],{},[377,1922,1923],{},[235,1924,1925],{"href":706},"How to write to a file in Python",[377,1927,1928],{},[235,1929,1930],{"href":710},"How to append to a file in Python",[377,1932,1933],{},[235,1934,1876],{"href":1875},[377,1936,1937],{},[235,1938,991],{"href":990},[377,1940,1941],{},[235,1942,1943],{"href":1679},"How to handle exceptions in Python",[377,1945,1946],{},[235,1947,1685],{"href":1684},[14,1949,1950],{},"Try extending this script in one of your own projects by logging user actions, important steps, or errors.",[1952,1953,1954],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":35,"searchDepth":59,"depth":59,"links":1956},[1957,1960,1961,1964,1965,1966,1967,1970,1976],{"id":365,"depth":59,"text":366,"children":1958},[1959],{"id":668,"depth":66,"text":669},{"id":714,"depth":59,"text":715},{"id":748,"depth":59,"text":749,"children":1962},[1963],{"id":775,"depth":66,"text":776},{"id":994,"depth":59,"text":995},{"id":1257,"depth":59,"text":1258},{"id":1366,"depth":59,"text":1367},{"id":1688,"depth":59,"text":1689,"children":1968},[1969],{"id":1712,"depth":66,"text":1713},{"id":1879,"depth":59,"text":1880,"children":1971},[1972,1973,1974,1975],{"id":1883,"depth":66,"text":1884},{"id":1892,"depth":66,"text":1893},{"id":1899,"depth":66,"text":1900},{"id":1906,"depth":66,"text":1907},{"id":1917,"depth":59,"text":1918},"Master python simple logging system example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-simple-logging-system-example",{"title":5,"description":1977},"examples\u002Fpython-simple-logging-system-example","GQK6gpLUJ34Shi4bqeCtjF60_npfuyey6nECuLnScjs",1777585477990]