Skip to content

Commit 10457be

Browse files
authored
Merge pull request mouredev#4409 from qwik-zgheib/main
#25 - Python|Go
2 parents 37b7b07 + 42092de commit 10457be

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

Roadmap/25 - LOGS/go/qwik-zgheib.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"time"
7+
)
8+
9+
// -- exercise
10+
func setupLogging() {
11+
logFile, err := os.OpenFile("log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
12+
if err != nil {
13+
log.Fatal("Failed to open log file:", err)
14+
}
15+
16+
log.SetOutput(logFile)
17+
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
18+
}
19+
20+
// -- extra challenge
21+
type Task struct {
22+
Name string
23+
Description string
24+
}
25+
26+
type TaskManager struct {
27+
Tasks []Task
28+
}
29+
30+
func (tm *TaskManager) AddTask(name, description string) {
31+
start := time.Now()
32+
tm.Tasks = append(tm.Tasks, Task{Name: name, Description: description})
33+
elapsed := time.Since(start)
34+
log.Printf("Task '%s' added successfully. Elapsed time: %s", name, elapsed)
35+
}
36+
37+
func (tm *TaskManager) DeleteTask(name string) {
38+
start := time.Now()
39+
for i, task := range tm.Tasks {
40+
if task.Name == name {
41+
tm.Tasks = append(tm.Tasks[:i], tm.Tasks[i+1:]...)
42+
elapsed := time.Since(start)
43+
log.Printf("Task '%s' deleted successfully. Elapsed time: %s", name, elapsed)
44+
return
45+
}
46+
}
47+
log.Printf("Task '%s' not found.", name)
48+
}
49+
50+
func (tm *TaskManager) ListTasks() {
51+
start := time.Now()
52+
if len(tm.Tasks) == 0 {
53+
log.Println("No tasks found.")
54+
return
55+
}
56+
log.Println("Tasks:")
57+
for _, task := range tm.Tasks {
58+
log.Printf("Name: %s, Description: %s", task.Name, task.Description)
59+
}
60+
elapsed := time.Since(start)
61+
log.Printf("Listed all tasks. Elapsed time: %s", elapsed)
62+
}
63+
64+
func main() {
65+
setupLogging()
66+
67+
log.Println("This is a log message.")
68+
log.Printf("This is a log message with formatted output: %s", "Hello, World!")
69+
70+
log.Println("This is an informational message.")
71+
72+
log.Printf("This is an informational message with formatted output: %s", "Hello, World!")
73+
74+
75+
log.Println("This is a warning message.")
76+
77+
log.Printf("This is a warning message with formatted output: %s", "Hello, World!")
78+
79+
log.Println("This is an error message.")
80+
log.Printf("This is an error message with formatted output: %s", "Hello, World!")
81+
82+
log.Println("This is a fatal message.")
83+
log.Printf("This is a fatal message with formatted output: %s", "Hello, World!")
84+
log.Fatal("Fatal error occurred.")
85+
86+
taskManager := TaskManager{}
87+
88+
taskManager.AddTask("Task 1", "Description 1")
89+
taskManager.AddTask("Task 2", "Description 2")
90+
taskManager.AddTask("Task 3", "Description 3")
91+
92+
taskManager.ListTasks()
93+
94+
taskManager.DeleteTask("Task 2")
95+
96+
taskManager.ListTasks()
97+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import logging
2+
3+
# -- exercise
4+
logging.basicConfig(level=logging.DEBUG)
5+
6+
# Log messages with different severity levels
7+
logging.debug("This is a debug message")
8+
logging.info("This is an info message")
9+
logging.warning("This is a warning message")
10+
logging.error("This is an error message")
11+
logging.critical("This is a critical message")
12+
13+
14+
# -- extra challenge
15+
class TaskManager:
16+
def __init__(self):
17+
self.tasks = []
18+
19+
def add_task(self, name, description):
20+
task = {"name": name, "description": description}
21+
self.tasks.append(task)
22+
logging.info(f"Task '{name}' added")
23+
24+
def delete_task(self, name):
25+
for task in self.tasks:
26+
if task["name"] == name:
27+
self.tasks.remove(task)
28+
logging.info(f"Task '{name}' deleted")
29+
break
30+
else:
31+
logging.warning(f"Task '{name}' not found")
32+
33+
def list_tasks(self):
34+
for task in self.tasks:
35+
logging.info(f"Task: {task['name']}, Description: {task['description']}")
36+
37+
38+
task_manager = TaskManager()
39+
40+
task_manager.add_task("Task 1", "Description 1")
41+
task_manager.add_task("Task 2", "Description 2")
42+
43+
task_manager.delete_task("Task 1")
44+
45+
task_manager.list_tasks()

0 commit comments

Comments
 (0)