Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Add 03.2 - log package
Browse files Browse the repository at this point in the history
  • Loading branch information
parsiya committed Dec 26, 2017
1 parent 86f9e07 commit cdbf68e
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ These documents are based on the `Gray/Black Hat Python/C#` series of books. I l
+ [02.6 - Goroutines and channels](content/02.6.md)
+ [02.7 - Error handling](content/02.7.md)
- [03 - Useful Go packages - WIP](content/03.0.md)
+ [03.1 - flag](content/03.1.md)
+ [03.1 - flag package](content/03.1.md)
+ [03.2 - log package](content/03.2.md)
- [04- Go networking](content/04.0.md)
+ [04.1 - Basic TCP and UDP clients](content/04.1.md)
+ [04.2 - TCP servers](content/04.2.md)
Expand Down
14 changes: 14 additions & 0 deletions code/03/03.2/03.2-01-basic-logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"log"
)

func main() {

a, b := 10, 20

log.Print("Use Print to log.")
log.Println("Ditto for Println.")
log.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b)
}
27 changes: 27 additions & 0 deletions code/03/03.2/03.2-02-log-file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"log"
"os"
)

func main() {

// Create a file
logFile, err := os.Create("log1.txt")
if err != nil {
panic("Could not open file")
}

// Close the file after main returns
defer logFile.Close()

a, b := 10, 20

// We will not use the other options
myLog := log.New(logFile, "", 0)

myLog.Print("Use Print to log.")
myLog.Println("Ditto for Println.")
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b)
}
47 changes: 47 additions & 0 deletions code/03/03.2/03.2-03-log-multiple-files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"bytes"
"fmt"
"io"
"log"
"os"
)

func main() {

// Create a file
logFile, err := os.Create("log1.txt")
if err != nil {
panic("Could not open file")
}

// Close the file after main returns
defer logFile.Close()

// Create a second file
logFile2, err := os.Create("log2.txt")
if err != nil {
panic("Could not open file2")
}

defer logFile2.Close()

// Create a buffer
var buflog bytes.Buffer

multiW := io.MultiWriter(logFile, logFile2, &buflog, os.Stdout)

a, b := 10, 20

// Log to multiW
myLog := log.New(multiW, "", 0)

myLog.Print("Use Print to log.")
myLog.Println("Ditto for Println.")
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b)

// Print buffer
fmt.Println("Buffer:")
fmt.Println(buflog.String())
}
19 changes: 19 additions & 0 deletions code/03/03.2/03.2-04-log-flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"log"
"os"
)

func main() {

a, b := 10, 20

// New logger will output to stdout with flags
// Only log date and file
myLog := log.New(os.Stdout, "", log.Ldate|log.Lshortfile)

myLog.Print("Use Print to log.")
myLog.Println("Ditto for Println.")
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b)
}
19 changes: 19 additions & 0 deletions code/03/03.2/03.2-05-log-prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"log"
"os"
)

func main() {

a, b := 10, 20

// New logger will output to stdout with prefix "Log1: " and flags
// Note the space in prefix
myLog := log.New(os.Stdout, "Log1: ", log.Ldate|log.Lshortfile)

myLog.Print("Use Print to log.")
myLog.Println("Ditto for Println.")
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b)
}
3 changes: 3 additions & 0 deletions code/03/03.2/log1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use Print to log.
Ditto for Println.
Use Printf for format strings. 10 + 20 = 30
3 changes: 3 additions & 0 deletions code/03/03.2/log2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use Print to log.
Ditto for Println.
Use Printf for format strings. 10 + 20 = 30
4 changes: 2 additions & 2 deletions content/03.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ This is where this repo is going to divert a bit from BH books. This section is

As I move forward and learn more, I will return and add more tutorials here.

- [03.1 - flag](03.1.md)

- [03.1 - flag package](03.1.md): Parsing command line parameters.
- [03.2 - log package](03.2.md): Logging.
8 changes: 4 additions & 4 deletions content/03.1.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flag
# flag package
[flag package][flag-pkg] is the Go equivalent of Python [argparse][python-argparse]. While not as powerful, it does what we expect it to do. It simplifies adding and parsing command line parameters, leaving us to concentrate on the tools. Most of our tools will need them to be actually useful (hardcoding URLs and IPs get old too fast).

<!-- MarkdownTOC -->
Expand Down Expand Up @@ -510,10 +510,10 @@ As you can see there's a lot of manual work in sub commands and they are not as

<!-- Links -->

[flag-pkg]: https://golang.org/pkg/flag/
[flag-pkg]: https://godoc.org/flag
[python-argparse]: https://docs.python.org/2/howto/argparse.html
[flag-value-interface]: https://golang.org/pkg/flag/#Value
[flag-value-interface]: https://godoc.org/flag#Value
[sync-waitgroup]: 02.6.md#syncwaitgroup
[flag-newflagset]: https://golang.org/pkg/flag/#NewFlagSet
[flag-newflagset]: https://godoc.org/flag#NewFlagSet
[cobra-github]: https://github.com/spf13/cobra
[cli-github]: https://github.com/urfave/cli
Loading

0 comments on commit cdbf68e

Please sign in to comment.