示例#1
0
func Fetch(url string) (string, error) {
	t := log.Start()
	defer log.End(t)

	sum := md5.Sum([]byte(fmt.Sprintf("%s-%d", url, time.Now().UnixNano())))
	f := fmt.Sprintf("%x", sum)
	filename := path.Join(tempDir, f)
	log.Debugf("file is temporary saved as %s", filename)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Errorf("fail to new request: error=%v", err)
		return "", err
	}
	req.Header.Set("User-Agent", UserAgent)
	resp, err := client.Do(req)
	if err != nil {
		log.Errorf("fail to GET %s: error=%v", url, err)
		return "", err
	}

	dump, _ := httputil.DumpRequest(req, true)
	log.Debugf("%s", dump)

	if resp.StatusCode != http.StatusOK {
		log.Printf("not ok: StatusCode=%d", resp.StatusCode)
		return "", fmt.Errorf("can't fetch image %s", url)
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			log.Error(err)
		}
	}()
	log.Printf("ok: StatusCode=%d", resp.StatusCode)

	file, err := os.Create(filename)
	defer func() {
		if err := file.Close(); err != nil {
			log.Error(err)
		}
	}()
	if err != nil {
		return "", err
	}
	if _, err := io.Copy(file, resp.Body); err != nil {
		return "", err
	}

	return filename, nil
}
示例#2
0
func TestStartAndEnd(t *testing.T) {
	os.Setenv(log.EnvLogFilename, file)
	filename, err := log.Init()
	if err != nil {
		t.Fatalf("fail to init: error=%v", err)
	}
	if err := os.Unsetenv(log.EnvLogFilename); err != nil {
		t.Fatalf("fail to unset env: error=%v", err)
	}

	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	timer := log.Start()

	log.Debug("debug")
	log.Print("print")
	log.Info("info")
	log.Warn("warn")
	log.Warning("warning")
	log.Error("error")

	log.Debugf("debug%s", "f")
	log.Printf("print%s", "f")
	log.Infof("info%s", "f")
	log.Warnf("warn%s", "f")
	log.Warningf("warning%s", "f")
	log.Errorf("error%s", "f")

	log.Debugln("debugln")
	log.Println("println")
	log.Infoln("infoln")
	log.Warnln("warnln")
	log.Warningln("warningln")
	log.Errorln("errorln")

	log.End(timer)

	expected := []map[string]string{
		map[string]string{"level": "info", "msg": "start"},

		map[string]string{"level": "info", "msg": "print"},
		map[string]string{"level": "info", "msg": "info"},
		map[string]string{"level": "warning", "msg": "warn"},
		map[string]string{"level": "warning", "msg": "warning"},
		map[string]string{"level": "error", "msg": "error"},

		map[string]string{"level": "info", "msg": "printf"},
		map[string]string{"level": "info", "msg": "infof"},
		map[string]string{"level": "warning", "msg": "warnf"},
		map[string]string{"level": "warning", "msg": "warningf"},
		map[string]string{"level": "error", "msg": "errorf"},

		map[string]string{"level": "info", "msg": "println"},
		map[string]string{"level": "info", "msg": "infoln"},
		map[string]string{"level": "warning", "msg": "warnln"},
		map[string]string{"level": "warning", "msg": "warningln"},
		map[string]string{"level": "error", "msg": "errorln"},

		map[string]string{"level": "info", "msg": "end"},
	}

	bufs, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Fatalf("fail to read file: error=%v", err)
	}
	lines := strings.Split(string(bufs), "\n")

	l := len(lines)
	for i, line := range lines {
		// fmt.Println(i, line)

		if i == l-1 {
			break
		}
		var obj map[string]interface{}
		if err := json.Unmarshal([]byte(line), &obj); err != nil {
			t.Fatalf("fail to decode json")
		}

		e := expected[i]
		// fmt.Println(i, e)
		if obj["level"] != e["level"] {
			t.Fatalf("wrong level: expected %s, but actual %s", e["level"], obj["level"])
		}
		if obj["msg"] != e["msg"] {
			t.Fatalf("wrong msg: expected %s, but actual %s", e["msg"], obj["msg"])
		}
	}

	if err := log.Close(); err != nil {
		t.Fatalf("fail to close log: error=%v", err)
	}
}