"How and when did the letter Z become to be associated with sleeping?"
First of all, zzzz (or z-z-z-z) is sound of snoring, from at least 1918. (Sometimes "a tiny saw cutting through a log" [1948] would be used, and both the snore and saw would make the same z-z-z-z sound.) Over time, this became associated with sleep in general, but most comic reference books (e.g. 2006's KA-BOOM! A Dictionary of Comic Book Words, Symbols & Onomatopoeia, 2008's Comic books: how the industry works) still mainly associate it with snoring.
See also Why Does ZZZ mean sleep? for another theory:
The reason zzz came into being is that the
comic strip artists just couldn’t
represent sleeping with much. ... As
the sounds made while sleeping are
quite difficult to represent with
letters, the artists chose zzz,
because it best represents the sound
... In fact it has made itself into
its own meaning - it no longer needs
explanation and is generally accepted
world wide as a representation of
sleeping. The reason it even became
what it now is, is almost lost, such
as the meaning of the wrong end of the
stick. I’ll let you figure that one
out.
The earliest references I found connect zzzz with snoring, including an explanation that says snoring is indicated by zzzz.
American Dialect Society's Dialect notes, Volume 5, 1918:
z-z-z (buzzing, or snoring)
Boys' Life in Jan 1919, and The Boy Scouts' year book also in 1919:
Z-z-z-z-z-z-z-z-z
Tenderfoot: "Do you know the scientific name for snoring?"
Eagle Scout: "Uh, no."
Tenderfoot: "Sheet Music."
Life magazine, Volume 79 in 1922:
Snoring is indicated by "ZZZZZZZ."
which often is supplemented by a.
picture like this: Profanity may be
conveyed by a series of punctuation
marks and hieroglyphics, such as :
?;!f
Boys' Life in Oct 1927, recycling their jokes:
Z-Z-Z-Z
Tenderfoot: "Do you know the scientific name for snoring?"
Eagle Scout: "Why, no."
Tenderfoot: "Sheet music."
Contact point by the University of the Pacific, School of Dentistry in 1927:
ZZZZZZ (snoring)— Heard during
lecture. 2. The Scolenius Medius. the
largest and longest of the three
Scoleni. arises from the posterior
tubercle of the transverse processes
of the lower six cervical vertebrae,
and descending along, ...
Boys Life yet again in Nov 1928, this time with an illustration:
zzzzzzz
One: What is snoring?
Two: Sheet music.
Illinois education, Volume 42 in 1953:
So saying, Tooten hitched his falling
socks, yawned, shut the door on his
intellectual rescrvdir now devoid of
any running comment, and went back to
sleep.
Calico called her first grade
to order. "Zzzzzzz," quavered she, "is
the sound of the bee, the one with
wings, but stands for Z. Congusing,
isn't it?"
Zzzzzzz snored president
Timothy.
Instructor, Volume 67 in 1957:
A little girl who had learned her
alphabet began by Awakening and
followed the alphabet all through the
day, each letter representing many
things, until she went to sleep,
Zzzzzz.
My name is--: a game of letters and their sounds by Lois Baker Muehl in 1959:
This may sound crazy, but even in the
breeze the zigzag cages will seem
cozy. They will all be the right kind
and size. And at night I will play a
zither to help all the animals go to
sleep and snooze, Zzzzz
Edit: Another Wikipedia page:
The big Z
It is a convention in American comics
that the sound of a snore can be
reduced to a single letter Z. Thus a
speech bubble with this letter
standing all alone (again, drawn by
hand rather than a font type) means
the character is sleeping in most
humorous comics. This can be seen, for
instance, in Charles Schulz's Peanuts
comic strips.
Being such a long-established device,
the Z-bubble does not even imply that
the character is snoring anymore, but
just sleeping. Jim Davis has based
some jokes starring Garfield upon this
technique; for example, in one strip,
Garfield is unable to sleep because
his Z-bubble is pointing in the wrong
direction. When he grabs the bubble's
tail to make it point at himself, he
falls asleep.
Originally, the resemblance between
the 'z' sound and that of a snore
seemed exclusive to the English
language, but the spread of American
comics has made it a frequent feature
in other countries. An exception to
this is in Japanese manga, where the
usual symbol for sleep is a large
bubble coming out of the character's
nose.