本文整理匯總了Golang中github.com/koding/logging.Logger.SetHandler方法的典型用法代碼示例。如果您正苦於以下問題:Golang Logger.SetHandler方法的具體用法?Golang Logger.SetHandler怎麽用?Golang Logger.SetHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/koding/logging.Logger
的用法示例。
在下文中一共展示了Logger.SetHandler方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UpdateCommand
// UpdateCommand updates this binary if there's an update available.
func UpdateCommand(c *cli.Context, log logging.Logger, _ string) int {
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "update")
return 1
}
var (
forceUpdate = c.Bool("force")
klientVersion = c.Int("klient-version")
klientChannel = c.String("klient-channel")
kdVersion = c.Int("kd-version")
kdChannel = c.String("kd-channel")
continueUpdate = c.Bool("continue")
)
if kdChannel == "" {
kdChannel = config.Environment
}
if klientChannel == "" {
klientChannel = config.Environment
}
// Create and open the log file, to be safe in case it's missing.
f, err := createLogFile(LogFilePath)
if err != nil {
fmt.Println(`Error: Unable to open log files.`)
} else {
log.SetHandler(logging.NewWriterHandler(f))
log.Info("Update created log file at %q", LogFilePath)
}
if !shouldTryUpdate(kdVersion, klientVersion, forceUpdate) {
yesUpdate, err := checkUpdate()
if err != nil {
log.Error("Error checking if update is available. err:%s", err)
fmt.Println(FailedCheckingUpdateAvailable)
return 1
}
if !yesUpdate {
fmt.Println("No update available.")
return 0
} else {
fmt.Println("An update is available.")
}
}
if kdVersion == 0 {
var err error
kdVersion, err = latestVersion(config.Konfig.Endpoints.KDLatest.Public.String())
if err != nil {
log.Error("Error fetching klientctl update version. err: %s", err)
fmt.Println(FailedCheckingUpdateAvailable)
return 1
}
}
if klientVersion == 0 {
var err error
klientVersion, err = latestVersion(config.Konfig.Endpoints.KlientLatest.Public.String())
if err != nil {
log.Error("Error fetching klient update version. err: %s", err)
fmt.Println(FailedCheckingUpdateAvailable)
return 1
}
}
klientPath := filepath.Join(KlientDirectory, "klient")
klientctlPath := filepath.Join(KlientctlDirectory, "kd")
klientUrl := config.S3Klient(klientVersion, klientChannel)
klientctlUrl := config.S3Klientctl(kdVersion, kdChannel)
// If --continue is not passed, download kd and then call the new kd binary with
// `kd update --continue`, so that the new code handles updates to klient.sh,
// service, and any migration code needed.
if !continueUpdate {
// Only show this message once.
fmt.Println("Updating...")
if err := downloadRemoteToLocal(klientctlUrl, klientctlPath); err != nil {
log.Error("Error downloading klientctl. err:%s", err)
fmt.Println(FailedDownloadUpdate)
return 1
}
flags := flagsFromContext(c)
// Very important to pass --continue for the subprocess.
// --force also helps ensure it updates, since the subprocess is technically
// the latest KD version.
flags = append([]string{"update", "--continue=true", "--force=true"}, flags...)
log.Info("%s", flags)
cmd := exec.Command(klientctlPath, flags...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Error("error during --continue update. err: %s", err)
//.........這裏部分代碼省略.........
示例2: InstallCommandFactory
// The implementation of InstallCommandFactory, with an error return. This
// allows us to track the error metrics.
func InstallCommandFactory(c *cli.Context, log logging.Logger, _ string) (exit int, err error) {
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "install")
return 1, errors.New("incorrect cli usage: no args")
}
// Now that we created the logfile, set our logger handler to use that newly created
// file, so that we can log errors during installation.
f, err := createLogFile(LogFilePath)
if err != nil {
fmt.Println(`Error: Unable to open log files.`)
} else {
log.SetHandler(logging.NewWriterHandler(f))
log.Info("Installation created log file at %q", LogFilePath)
}
// Track all failed installations.
defer func() {
if err != nil {
log.Error(err.Error())
metrics.TrackInstallFailed(err.Error(), config.VersionNum())
}
}()
authToken := c.Args().Get(0)
// We need to check if the authToken is somehow empty, because klient
// will default to user/pass if there is no auth token (despite setting
// the token flag)
if strings.TrimSpace(authToken) == "" {
cli.ShowCommandHelp(c, "install")
return 1, errors.New("incorrect cli usage: missing token")
}
// Create the installation dir, if needed.
if err := os.MkdirAll(KlientDirectory, 0755); err != nil {
log.Error(
"Error creating klient binary directory(s). path:%s, err:%s",
KlientDirectory, err,
)
fmt.Println(FailedInstallingKlient)
return 1, fmt.Errorf("failed creating klient binary: %s", err)
}
klientBinPath := filepath.Join(KlientDirectory, "klient")
// TODO: Accept `kd install --user foo` flag to replace the
// environ checking.
klientSh := klientSh{
User: konfig.CurrentUser.Username,
KlientBinPath: klientBinPath,
}
if err := klientSh.Create(filepath.Join(KlientDirectory, "klient.sh")); err != nil {
err = fmt.Errorf("error writing klient.sh file: %s", err)
fmt.Println(FailedInstallingKlient)
return 1, err
}
fmt.Println("Downloading...")
version, err := latestVersion(config.Konfig.Endpoints.KlientLatest.Public.String())
if err != nil {
fmt.Printf(FailedDownloadingKlient)
return 1, fmt.Errorf("error getting latest klient version: %s", err)
}
if err := downloadRemoteToLocal(config.S3Klient(version, config.Environment), klientBinPath); err != nil {
fmt.Printf(FailedDownloadingKlient)
return 1, fmt.Errorf("error downloading klient binary: %s", err)
}
fmt.Printf("Created %s\n", klientBinPath)
fmt.Printf(`Authenticating you to the %s
`, config.KlientName)
cmd := exec.Command(klientBinPath, "-register",
"-token", authToken,
"--kontrol-url", strings.TrimSpace(c.String("kontrol")),
)
var errBuf bytes.Buffer
// Note that we are *only* printing to Stdout. This is done because
// Klient logs error messages to Stderr, and we want to control the UX for
// that interaction.
//
// TODO: Logg Klient's Stderr message on error, if any.
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = &errBuf
if err := cmd.Run(); err != nil {
err = fmt.Errorf("error registering klient: %s, klient stderr: %s", err, errBuf.String())
fmt.Println(FailedRegisteringKlient)
return 1, err
}
//.........這裏部分代碼省略.........