Amazing how Kenyan judges still think that their job is to determine guilt and mete out punishment as possible.
And arrested, taken to court, and sentenced within a couple of days, both during a major holiday period when all sorts of people and services are unavailable! Incredible.
The first issue that arises here is whether the guy had any (or adequate) legal representation. That is the first thing a judge ought to ensure---that the person is properly represented and understand his plea, guilty or not-guilty. (That is especially important where claims have been made about mental health.) Even without mental issues, people sometimes do not understand the difference between admitting they did something and admitting they committed a crime; in this case, the guy may just have thought that the question was whether he had written the stuff or not, which is not the same as whether he had committed the charged crimes.
Subsequently, on a finding or a plea of guilt, there should be a proper pre-sentencing hearing (or at least the opportunity for the person, via legal representation, to make other submissions). That too is especially important when issues of mental health have been raised.
Another problem with Kenya's court system is that there are no mandatory (or near-mandatory) sentencing guidelines that judges are expected to follow. Kenyan judges think that if a sentence is permitted, then they should be free to impose it, and they routinely give out the most absurd of sentences. In many judicial systems---the USA, Canada, and the UK as examples of common law---judges do not have the freedom to sentence willy-nilly, and various guidelines make it hard to impose excessive sentences for relatively minor things.